Jump to content

Sessions are killing me


kevinak

Recommended Posts

Ok, I've searched and searched but I can't figure out how to make sessions work for what I need

 

We have two pages.

1. Confirm page

2. Reward Page

 

I tried setting a session on the Confirm page with this code.

<?php
$_SESSION['OMG']= 'on';
session_start();
?>

 

I think that's right. Now I need to call that session and check if it is set, if it is then I want the user to get the reward on the rewards page. If it's not I want an error to come up.

Then I need the session to destory itself or reset to nothing so they can't refresh or simply change the url to get a different reward without going through the confirm page.

 

In other words. I want them to first go to the confirm page at all times. No way around it.

 

<?php
session_name();
session_start();
?>

 

This is what I used to call the sessions on the Rewards Page. (I'm very new to sessions)

 

Then my if is simply this

if (!isset($_SESSION['OMG'])) {
//error here
}
else {
//reward here
}

 

Thank you for reading and for any responses

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/122562-sessions-are-killing-me/
Share on other sites

session_start() has to be before you do pretty much anything with sessions..

 

session_start();
$_SESSION['foo'] = 'bar';

 

then, on a later page you call it after session_start();

 

session_start();
echo $_SESSION['foo'];

 

as for your implementation, hopefully you can derive it from this explanation of sessions.

confirm page

<?php
session_start(); //session_start() comes first
$_SESSION['confirmed'] = 'yeah baby';
?>

 

reward page

<?php
session_start(); //session_start() comes first here too
if(isset($_SESSION['confirmed'])){
    echo 'yeah baby. here is your award';
} else{
    echo 'nah, youre cheating';
}
?>

 

EDIT: genericnumber1 beat me to it, but i'm posting this anyway

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.