By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,967 Members | 1,687 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,967 IT Pros & Developers. It's quick & easy.

Problem with HTTP_REFERER when clicking on refresh button

P: n/a
In a simple webshop application I am trying to check that the "shopping
cart" only should be "filled" when you choose an article in "meerinfo.php"
and click on a link to "winkelwagen.php". It works fine in that sense that
when I would enter in the browser "www.mysite.php/winkelwagen?id=5"it will
be ignored.
However when I go from meerinfo.php to winkelwagen.php , the article is
added again and again to the shopping cart when I click on the refresh
button in Internet Explorer. Is there a way to avoid that?

This is the code in winkelwagen.php

if(!isset($_SESSION['artikelen'])) //initiate the array when the first
article is added to the shopping cart
{
$_SESSION['artikelen'] = array();
}
$waarvandaan = $_SERVER['HTTP_REFERER'];
if (strstr($waarvandaan, "meerinfo.php"))
{
$id = $_GET['id'];
$_SESSION['artikelen'][] = $id;
}

Thanks for any help (or suggestions for articles that discuss this topic)

Martien

May 13 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Martien van Wanrooij wrote:
In a simple webshop application I am trying to check that the "shopping
cart" only should be "filled" when you choose an article in
"meerinfo.php" and click on a link to "winkelwagen.php". It works fine
in that sense that when I would enter in the browser
"www.mysite.php/winkelwagen?id=5"it will be ignored.
However when I go from meerinfo.php to winkelwagen.php , the article is
added again and again to the shopping cart when I click on the refresh
button in Internet Explorer. Is there a way to avoid that?

This is the code in winkelwagen.php

if(!isset($_SESSION['artikelen'])) //initiate the array when the first
article is added to the shopping cart
{
$_SESSION['artikelen'] = array();
}
$waarvandaan = $_SERVER['HTTP_REFERER'];
if (strstr($waarvandaan, "meerinfo.php"))
{
$id = $_GET['id'];
$_SESSION['artikelen'][] = $id;
}

Thanks for any help (or suggestions for articles that discuss this topic)

Martien
Martien,

It's doing exactly what you told it to do. Every time you hit refresh
on your browser, it's exactly the same as clicking the "ad" button on
the previous page.

Rather - when you click to add an item on the first page, make it go to
an intermediate script. All that script does is add the item to the
shopping cart, then redirect (using the 'header' function) to the third
page (which displays the cart).
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
May 13 '07 #2

P: n/a
Hi Martien,

a header redirect is a simple way to prevent reloading a page several
times. Store your article in "winkelwagen.php" and make a redirect to
another page.
Example:
[snip]
if(!isset($_SESSION['artikelen'])) //initiate the array when the
first
article is added to the shopping cart
{
$_SESSION['artikelen'] = array();
}

$waarvandaan = $_SERVER['HTTP_REFERER'];
if (strstr($waarvandaan, "meerinfo.php"))
{
$id = $_GET['id'];
$_SESSION['artikelen'][] = $id;
}

header("Location: ./meerinfo.php");
exit;
[/snap]

The redirectiom should done before any output is send to client.

purcaholic
May 13 '07 #3

P: n/a

"Jerry Stuckle" <js*******@attglobal.netschreef in bericht
news:PM******************************@comcast.com. ..
It's doing exactly what you told it to do. Every time you hit refresh on
your browser, it's exactly the same as clicking the "ad" button on the
previous page.
Thank you Jerry, I thought that when I clicked on the refresh button the
http_referrer would be the page that was already in the browser (i.e. the
second page itself) and I will follow your suggestion to create an
intermediate script.

Martien.

May 13 '07 #4

P: n/a

"purcaholic" <pu********@googlemail.comschreef in bericht
news:11********************@w5g2000hsg.googlegroup s.com...
header("Location: ./meerinfo.php");
exit;
[/snap]

The redirectiom should done before any output is send to client.
Thank you purcaholic, both Jerry's and your suggestions give me a good idea
about how to handle it.
BTW the meerinfo.php in my example is a page that shows the article and that
contains a link to the shopping cart. I suppose that the procedure you
suggest is that "add to shopping cart" should be handled by a script
"cart_handler.php" and that "cart_handler.php" should be redirected to
"showshoppingcart.php" ?
One more question: is the "exit" statement required or advisable even when a
header(Location.. statement just has been executed?
Thx again,
Martien.

May 13 '07 #5

P: n/a
Martien van Wanrooij wrote:
One more question: is the "exit" statement required or advisable even
when a header(Location.. statement just has been executed?
Yes, required.
May 13 '07 #6

P: n/a
On Sun, 13 May 2007 18:36:08 +0300, Alexey Kulentsov <cr*******@crimaniak.com>
wrote:
>Martien van Wanrooij wrote:
>One more question: is the "exit" statement required or advisable even
when a header(Location.. statement just has been executed?

Yes, required.
No, it's just advisable.
--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
May 13 '07 #7

P: n/a
"Martien van Wanrooij" <i...@martienvanwanrooij.nlwrote:
... I suppose that the procedure you
suggest is that "add to shopping cart" should be handled by a script
"cart_handler.php" and that "cart_handler.php" should be redirected to
"showshoppingcart.php" ?
One more question: is the "exit" statement required or advisable even when a
header(Location.. statement just has been executed?
Thats a good idea.
Use a "cart_handler.php" script, to add an article and redirect to
"showshoppingcart.php" which just displays content of shopping cart.

You can also use same Script to handle action for adding article to
cart and to displaying it. If an paramter "id" is send, add article to
session, redirect to itself without "id" parameter. If no parameter
"id" is send, display shoppingcart.

purcaholic

May 13 '07 #8

P: n/a
Martien van Wanrooij wrote:
>
"purcaholic" <pu********@googlemail.comschreef in bericht
news:11********************@w5g2000hsg.googlegroup s.com...
>header("Location: ./meerinfo.php");
exit;
[/snap]

The redirectiom should done before any output is send to client.
Thank you purcaholic, both Jerry's and your suggestions give me a good
idea about how to handle it.
BTW the meerinfo.php in my example is a page that shows the article and
that contains a link to the shopping cart. I suppose that the procedure
you suggest is that "add to shopping cart" should be handled by a script
"cart_handler.php" and that "cart_handler.php" should be redirected to
"showshoppingcart.php" ?
One more question: is the "exit" statement required or advisable even
when a header(Location.. statement just has been executed?
Thx again,
Martien.
That sounds like a good solution.

As for the exit() - it's not required in all circumstances. What it
does is stop execution of the script immediately. If, for instance, the
redirect is the last thing in the file, exit() doesn't do anything good
or bad. However, if it's earlier in the file and you have other code
which might be executed, you want to call it (if you wanted to execute
the rest of the code you shouldn't be calling header() this early, anyway).

So it's not absolutely required - but I normally put it in to ensure any
further code isn't executed. And I normally put it at the end, also -
just for consistency (and in case I add more stuff later).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
May 14 '07 #9

P: n/a
Martien van Wanrooij wrote:
In a simple webshop application I am trying to check that the "shopping
cart" only should be "filled" when you choose an article in "meerinfo.php"
and click on a link to "winkelwagen.php". It works fine in that sense that
when I would enter in the browser "www.mysite.php/winkelwagen?id=5"it will
be ignored.
Don't use HTTP Referer checking like this. It's an optional HTTP header,
and some browsers do not send it; and some proxies filter it out, or send
fake referers.

If you really want to force users to go via page X to get to page Y, then
use sessions -- that's what they're there for.

--
Toby A Inkster BSc (Hons) ARCS
http://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux
May 14 '07 #10

P: n/a

"Toby A Inkster" <us**********@tobyinkster.co.ukschreef in bericht
news:mh************@ophelia.g5n.co.uk...
Don't use HTTP Referer checking like this. It's an optional HTTP header,
and some browsers do not send it; and some proxies filter it out, or send
fake referers.
Thank you Toby, I think you have a point there, I just tried the HTTP
Referer because I didn't know how to handle the problem I originally posted
but thanks to the good suggestions I got I have been able to resolve the
problem without them.

Martien.

May 14 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.