Solved

simply question about SESSIONS

Posted on 2002-03-08
7
173 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
Comment Utility
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
Comment Utility
Batalf:) hello!
Well, i am exspecting sample with sessions!!!
0
 
LVL 4

Expert Comment

by:daluu
Comment Utility
i'm confused. what's your question?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 9

Accepted Solution

by:
waygood earned 20 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now