PDA

View Full Version : Hide link !!


wap3
12-05-2002, 11:02 AM
Hi everyone

I got a question. I am building a shopping cart type system. Not a full blown but anyway here it is.

The user clicks on an item ( the link is in this format add_to_basket.php?action=add&itemid=23). They go to to shopping basket page where there selection is displayed. If they then delete that item with a button on this page. It deletes it then returns to the cart.

Now if they hit the browser back button then the item is added to the basket again because of the link.

How can I stop this from happening ?? Is it possible to perhaps change the url in the address bar after the 'add item to basket' button is clicked first of. So that when they do go back it doesn't add the item again ??

Any ideas on this one ??

Thanks

:thumbsup:

bcarl314
12-05-2002, 12:09 PM
you could use the 'HTTP_REFERER' to on your cart page to determine which page they came from, then disable the addItem call.

Some thing like

if($HTTP_REFERER=="deleteFromBasket.php") {
//do nothing
}
else {
addItem(itemName);
// or what ever your code is
}

wap3
12-05-2002, 02:19 PM
thanks bcarl,

but it doesn't seem to work. I don't get any errors or anything. Anyone know of anything else I could do or another trick ??

thanks

:thumbsup:

wap3
12-06-2002, 08:31 AM
anyone ??

here is and explanation and the code im using so you can see if there are any problems.

1.. user clicks link (add_to_basket.php?action=add&itemid=123)
2..they get taken to cart page
3..if the user then deletes the item from it here. it gets deleted then they get shown the empty-cart page.
3.. now if they press the browser back button it adds the item again because of the link.

This is the code for adding an item to the basket, as bcarl said but it dosn't work. Any ideas why it ??
(The first function is executed first)


function check_referer() {
if ($HTTP_REFERER == "../basket/empty-basket.htm") {
exit; }
else {
add_to_basket();
} }

function add_to_basket() {
if(isset($_GET['itemid'])) {
if(isset($_SESSION['cart'][$_GET['itemid']])) {
$message = "$_GET[itemid] was already in your cart. The quantity of this item can be changed below";
$_SESSION['cart'][$_GET['itemid']] = 1;
}
else {
$message = "$_GET[itemid] has been added to your basket.";
$_SESSION['cart'][$_GET['itemid']] = 1;
}
}
}


thanks

:confused:

bcarl314
12-06-2002, 01:08 PM
I think the referer will be an absolute URL (ie http://ww.mysitel.com/myPage.html") try changing you logic to accomidate that. Also, print out the $HTTP_REFERER variable to see what it is, that my offer so help.

ConfusedOfLife
12-06-2002, 01:20 PM
Yup, I also suggest that you check the refer(r!)er, sometimes it might be null!
Also I got something in your code:

function add_to_basket() {
if(isset($_GET['itemid'])) {
if(isset($_SESSION['cart'][$_GET['itemid']])) {
$message = "$_GET[itemid] was already in your cart. The quantity of this item can be changed below";
.
.
.



Do you think that if(isset($_SESSION['cart'][$_GET['itemid']])) is correct?

wap3
12-06-2002, 03:02 PM
no still no luck.

im testing this on my machine so i put the url as:

http://localhost/websitefolder/basket/empty-basket.htm

i also tried printing out the http-referer with:
echo "$HTTP_REFERER";
but it showed nothing.

Does this have be online to work ?? will it work with the browser back button which i have been talking about ??

any more ideas ??

If i can't get it to work, I will redirect the user to the catalogue section when the basket is empty (it only happens when empty). Instead of showing them the empty basket page. This is not want I really want but i guess it will be ok. They can still check the basket with a button on the main navigation bar and they should know if they deleted the only item in the basket it is now empty, right ??

::confusedoflife

I am new to php obviously. I did kind of think it should be there. I was not sure but seen as it worked I thought I would leave it.
Because it returns a true or false value I was thinking if it was true then the following code would be executed otherwise the next bit would (under the 'else') would be executed.

If I am wrong which I guess I am now please explain to me for future use (if you dont mind of course!!)

thanks

:thumbsup:

wap3
12-06-2002, 03:24 PM
oh ok,

so perhaps i might have put tried the wrong thing.
i put
$test = $_SESSION[HTTP_REFERER];
echo "$test"; and that outputted something when going to the page.

I just tried to see what the referer would be coming back from empty-basket.htm to cart page (pressing the browser back button) and it came up with what it shouldn't.

Anyway im just going to direct them to the catalogue page, when its empty.



:thumbsup:

ConfusedOfLife
12-06-2002, 11:44 PM
I think you should print out $_SERVER["HTTP_REFERER"] or $HTTP_SERVER_VARS["HTTP_REFERER"] .
As for as the code and the part that I quoted, I'm new to php too and I don't know if 2 dim sessions are accepted in PHP, but something is wrong for sure :


if(isset($_SESSION['cart'][$_GET['itemid']] )) {


Now if 2 dim sessions are not valid, then 2 things are wrong!

wap3
12-07-2002, 08:15 AM
oops yer i was suppose to write $_SERVER["HTTP_REFERER"] my mistake.

What does someone with php knowledge think about this codng issue ?? could i have coded it a little better ??

thanks

:thumbsup:

ConfusedOfLife
12-07-2002, 08:59 PM
It looks clear to me! Also it seems that we can have 2 dim sessions! it's cool!