gator
gator

Reputation: 3523

Finding the last page a user was on

I have four pages: cart.php, checkout1.php, checkout2.php, and checkout3.php. A summary of their function is below:

cart.php //display items and quantities in cart with option for coupon code
checkout1.php //input of user address
checkout2.php //order summary
checkout3.php //order placed into SQL

Things are kept in the cart via $_SESSION, but between each page I am using $_POST to bring information from one to the next. My concern is when a user just goes to www.site.com/checkout2.php from their address bar, for example, as it will display an empty page (but still allow them to continue to checkout3.php and waste SQL space). On each of the pages I redirect to index.php if the cart is empty (eg. you type in checkout2.php in the address bar on an empty cart, it won't display the bit of code that lets you continue), but if there are contents in $_SESSION but not $_POST, I'm not sure how to prevent users from screwing things up.

I thought maybe using something like isset(), but I would like something along the lines of (pseudocode):

//on checkout2.php
if (previouspage != "./checkout1.php") {
    echo "There was an error.";
} else {
    //display correct page
}

I know there exists $_SERVER['HTTP-REFERER'] or something along those lines, but my understanding is that not every browser supports this and it doesn't give a concise locationo of the referring page (eg. will say user came from www.site.com vs. www.site.com/checkout1.php)

Upvotes: 0

Views: 51

Answers (1)

trrrrrrm
trrrrrrm

Reputation: 11802

you can use a simple approach as follows:

1- create a session variable called steps

2- once the user moves from one step to the other increase the variable by 1

3- on the top of each page check the steps variable if it's valid for that page

For example:

Cart.php

$_SESSION['steps'] = 1;

checkout.php

if(isset($_SESSION["steps"]) && $_SESSION["steps"] == 1){
    //die or redirect
}

//once all the logic execute properly
$_SESSION['steps'] = 2;

checkout2.php

if(isset($_SESSION["steps"]) && $_SESSION["steps"] == 2){
    //die or redirect
}

//once all the logic execute properly
$_SESSION['steps'] = 3;

checkout3.php

if(isset($_SESSION["steps"]) && $_SESSION["steps"] == 3){
    //die or redirect
}

//once all the logic execute properly
unset($_SESSION['steps']);

Upvotes: 3

Related Questions