0

I am new to this, i am creating a site with a shopping cart. I have a main login page that already redirect the users to the homepage if they tries to login from this page, no issues with this. However i have a another page (checkout page) which direct the users to a login page if they have selected an item to purchased but have not yet sign in, after login from this page it genetically redirect to the homepage. How can make it so that it will go back to the checkout page if the users logs in from here?

if (count($_SESSION['prodquan']) != 0) { print "<table border='1' align='center' width='50%'>"; print "<tr><td colspan='5' style='color:red'>"; if (isset($_SESSION['username'])) print 'Hello ' . $_SESSION['username']; print "</td></tr>"; print "<caption style='font-size:20;color:red'>Shopping Cart</caption>"; print "<tr>"; print "<td>Product Id</td><td>Product Description</td><td>Quantity</td><td>Price</td><td>Cost</td></tr>"; foreach ($_SESSION['prodquan'] as $prod=>$quan) { $query = "select productdescription, price from product where productid = $prod"; $result = mysqli_query($conn, $query); $row = mysqli_fetch_array($result); $cost = $row[1]*$quan; print "<tr><td>$prod</td><td>$row[0]</td><td>$quan</td><td>$row[1]</td><td>$cost</td></tr>"; $_SESSION['totalcost'] += $cost; mysqli_free_result($result); } print "<tr align='right'><td colspan='4'>Total Cost:</td><td>" . $_SESSION['totalcost'] . "</td></tr>"; print "</table>"; print "<table border='0' width='50%' align='center'>"; print "<tr rowspan='2'>"; print "<td align='center'><a href='checkout.php?payment=yes'>Payment</a>&nbsp&nbsp<a href='validpage.php'>Products</a>&nbsp&nbsp<a href='logout.php'>logout</a></td>"; print "</tr>"; print "</table>"; } else { $errormessage = " The shopping cart is empty"; header("location:validpage.php?errormsg=$errormessage"); exit(); } if (@$_GET['payment'] == "yes") { if (!isset($_SESSION['username'])) { $errormessage = "Please login"; header("location:http://localhost/shoppingcart/?page_id=5?errormsg=$errormessage"); exit(); } //$_SESSION['payment'] = "yes"; print "<td align='center'><a href='checkout.php?credit=yes'>Credit Card</a>&nbsp&nbsp<a href='checkout.php?cheque=yes'>Cheque</a>&nbsp&nbsp<a href='checkout.php?american=yes'>American Express</a></td>"; //unset($_GET['payment']); } 

Many thanks

3 Answers 3

1

You can use $_SERVER['HTTP_REFERER'], to check if a user is coming from CHECKOUT page

IN LOGIN PAGE,

if(!empty($_SERVER['HTTP_REFERER'])){ if($_SERVER['HTTP_REFERER']) == 'YOUR CHECKOUT URL') //REDIRECT to CHECKOUT ie $_SERVER['HTTP_REFERER'] else //NORMAL HOME PAGE REDIRECT } 
Sign up to request clarification or add additional context in comments.

Comments

0

Simply pass a variable along with your initial redirect to the login page, something like:

header('Location : /login?intent=/checkout/'); 

then on your login page you can either store the $_GET['intent'] into the session, or store it as a hidden field in your form:

<form method="POST" action="/login"> <input type="hidden" name="intent" value="<?php echo !empty($_GET['intent']) ? htmlentities($_GET['intent']) : '/'; ?>" /> <input type="text" name="email" /> <input type="password" name="password" /> <input type="submit" value="Login" /> </form> 

Then, in your form processing code:

if (!empty($_GET['intent']) { header('Location: ' . $_GET['intent']); } 

Comments

0

You guys are champions! :-) i went with CodingAnt because the codes seem easier since i am just starting out but thanks for your input Jonathan. Cheers

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.