[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

simply question about SESSIONS

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
sviksna
Asked:
sviksna
1 Solution
 
BatalfCommented:
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
 
sviksnaAuthor Commented:
Batalf:) hello!
Well, i am exspecting sample with sessions!!!
0
 
daluuCommented:
i'm confused. what's your question?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
waygoodCommented:
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
 
waygoodCommented:
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
 
waygoodCommented:
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
 
sviksnaAuthor Commented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now