Solved

simply question about SESSIONS

Posted on 2002-03-08
7
207 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

679 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