Solved

simply question about SESSIONS

Posted on 2002-03-08
7
193 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

778 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