Jump to content


Photo

PHP Header Redirect URL


  • Please log in to reply
3 replies to this topic

#1 WebDeveloperMike

WebDeveloperMike

    New member

  • Member
  • 13 posts

Posted 30 January 2012 - 01:23 PM

Here is my question: Since the php engine reads code from top to bottom, why does the code below the header redirect continue to process once it has navigated away from the page? Here is my code:

<?php
require_once '../functions/functions.php';

header('Location: http://www.mikespot....ml/viewCart.php');

$shopping_cart = get_shopping_cart();


echo render_header();


$product_id = $_REQUEST['id'];

if (product_exists($product_id))
$shopping_cart->AddItem($product_id);



set_shopping_cart($shopping_cart);

echo render_paypal_checkout($shopping_cart);




echo render_footer();


END OF CODE -----------------------------------------------------------------------------------------

Thanks, Mike
  • 0

#2 Symphlion

Symphlion

    New member

  • New Members
  • 7 posts

Posted 30 January 2012 - 06:19 PM

Please explain. A header() forces the browser to go to a new page. It should stop executing that file inmediatly unless specified otherwise. If you don`t trust this, you can force the exit yourself like this:

header("Location: path/to/new/file.php"); exit();

It`s not a clean way of handling it, but it will stop executing that very page until PHP picks up on the new header location.

Just remember, headers() can ONLY be sent while there is no output! So if you echo something out and start using the header(), you will trigger an error with something like: Headers already sent.
  • 0

#3 WebDeveloperMike

WebDeveloperMike

    New member

  • Member
  • 13 posts

Posted 02 February 2012 - 02:02 AM

all of the code below the header does in fact work. ive used the shopping cart with paypal sandbox and live on paypal and it works exactly the same as before I put the header function code in. Its doing what i want it to do, so im glad it works, but im just confused because i thought for sure if I put the header code in there it wouldnt work - but it did. now im curious as to why. :rolleyes:
  • 0

#4 johnanderson

johnanderson

    Member

  • Member
  • PipPipPip
  • 63 posts

Posted 04 April 2012 - 06:05 AM

Using headers() method, you can easily transferred to the new page without having to click a link to continue. This is also useful for search engines.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users