Help with PHP Sessions

Hello all,

I’m building a test website which uses forms with the $_POST method. I’m building three pages, the first one where the user can select values from drop down boxes, like a quote page, then another page where that quote is passed on to another form to collect the users information like name, email, that sort of thing then finally a page where they can make a payment.

Anyway, I was reading up on PHP sessions and the basics but I could use a little help. I (think) I need to use them in order to stop users from lets say, jumping straight to page 2 or 3 instead of going in the correct order of filling in the first form, then second form, etc. Of course I don’t want someone just being able to go to the checkout page by typing in the URL and skipping the other two pages or something like that. (Hope that makes sense?)

So i’m assuming you use PHP sessions to stop people just jumping to certain pages right?
How exactly do I code something like this? I know its something like session_start(); but I got confused with the variables part and all the other stuff, so if someone could just help me a little that would be great! :slight_smile:

I hope I explained this well enough and I really apologize for making it so long! :wink:

Look at my last post on the following page. It is similar to what you are asking. It will give you a basic idea of how sessions work.

Thanks for the reply, I still need help, I think the example you showed me was a little too much. Currently I can’t get the sessions to work, its like it completely ignores them. Maybe it would help if I showed someone my code? Also, I’m not using mysql or anything like that, just PHP and HTML.

Lets go with as bare bones as we can get. Try this:
$_SESSION[‘test’] = ‘I am a Session’;
echo $_SESSION[‘test’];[/php]

Works fine, managed to get it to work now, had to mess around abit with something like:

<?php $price = $_POST['price']; $_SESSION['check'] = $_POST['Submitquote']; if (!isset($_SESSION['check'])) { header("Location:"); exit; } else { echo $price; } ?>


You have to always start the session. session_start()

here is a script that shows you how to work with global sessions, and private sessions (session cookies)
it should get you started.

// Get the private context
$private_id = session_id();
$b = $_SESSION[‘pr_key’];

// Get the global context

$a = $_SESSION['key'];

// Work & modify the global & private context (be ware of changing the global context!)


Test 2: Global Count is: <?=++$a?>

Test 2: Your Count is: <?=++$b?>

Private ID is <?=$private_id?>

Gloabl ID is <?=session_id()?>

        <?php print_r($_SESSION); ?>
<?php // Store it back session_name('Private'); session_id($private_id); session_start(); $_SESSION['pr_key'] = $b; session_write_close();

?> [/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service