Solved

simply question about SESSIONS

Posted on 2002-03-08
7
199 Views
Last Modified: 2006-11-17
Hello Experts!

Let's imagine i have a function vote(); wich adds a user's vote. So, my task is somehow to check, has user already woted or hasn't.

In simply for me it looks like

1) read value from session in to variable $session_voted;

2)

if($session_voted==false)
{ vote(); $session_voted=true; }
else
{ echo 'Sorry, you have already voted! ';}

3) save value of $session_voted in to the session;

Thanks!
0
Comment
Question by:sviksna
7 Comments
 
LVL 32

Expert Comment

by:Batalf
ID: 6851046
If you're using a submit-button to vote, you could have this check at the top of your file

Let's assume that the submit-button is called $submit

if($submit && !$session_voted)
{
 // add session
 set_cookie("session_voted","true");
 // Add data from the form to the db.


}
0
 
LVL 1

Author Comment

by:sviksna
ID: 6852490
Batalf:) hello!
Well, i am exspecting sample with sessions!!!
0
 
LVL 4

Expert Comment

by:daluu
ID: 6881667
i'm confused. what's your question?
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 9

Accepted Solution

by:
waygood earned 20 total points
ID: 6884233
Source Code as requested--

At the head of each page put the following, or just keep in a seperate file and include it.

-----------------------------
<?PHP
// Check for session
session_start();

// If no session id because no variables have been set-up
if (!PHPSESSID)
{
     session_register('voted');
}
else
{
// If session id but $voted has not been set-up, set it up
    if (!isset($voted))
    {
     session_register('voted');
    }
}
?>
------------------------------
0
 
LVL 9

Expert Comment

by:waygood
ID: 6884234
CONTINUED....

When you vote, test the $voted variable then set it a value

 if ($voted=='TRUE')
 {
  //display vote form here
  $voted='TRUE';
 }
 else
 {
   echo "you can't vote twice";
 }

Because it is registered there is no need to read and write to the session, it saves automatically.
0
 
LVL 9

Expert Comment

by:waygood
ID: 6884235
P.S. This will allow you to vote once per session. Opening another window then copying and pasting the url will allow you to vote again.

TO INCLUDE
Save the code at the top, in a file called inc_session.php
and at the top of your pages add in :-
include('inc_sessions.php');

                 ---WARNING---
DO NOT ADD BLANK LINES AND SPACES AFTER THE ?> IN ANY FILE YOU INCLUDE.

Sorry!!! but couldn't post in one go
0
 
LVL 1

Author Comment

by:sviksna
ID: 6884878
it's ok ;) just wonder :)) where u have been so long time ;))
2 weeks around ;)
i built on evning of that day, when asking a q. my own solution for multiple items, but ok, i accept ur's too :)
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question