Forum Moderators: coopster

Message Too Old, No Replies

Pass Get Variables Using PHP Header()

PHP Header()

         

renonet

4:48 am on Dec 1, 2004 (gmt 0)

10+ Year Member



Hi there,

I got a form page with 'update' & 'cancel' buttons. When 'cancel' clicked, I want to be redirected to the index page on the right page number.

URL of my edit page is like this:
[localhost...]

In the form.php I got few lines of code to get that 'cursor' before doing redirection:

$iCursor = $_GET["cursor"];
$url = 'index.php?cursor='.(int) $iCursor;

if ($_POST["cancel"]) {
header("Location: $url");
}

The new URL is supposed to be:
[localhost...]

but it showed up like this:
[localhost...]

I've tried using sessions variables as well, but the value of the cursor's still 0.

Do you guys know what happened?

Cheers,

Zipper

7:32 am on Dec 1, 2004 (gmt 0)

10+ Year Member



try without the (int)

kunwarbs

8:26 am on Dec 1, 2004 (gmt 0)

10+ Year Member



$iCursor = $_GET["cursor"];
$url = 'index.php?cursor='.(int) $iCursor;

if ($_POST["cancel"]) {
header("Location: $url");
}

==

instead of using if ($_POST["cancel"]), use
if ($_GET["cancel"])
since you are submitting the cancel value by get method

renonet

10:14 am on Dec 1, 2004 (gmt 0)

10+ Year Member



I've tried both methods, but still doesn't work.

Hanu

10:56 am on Dec 1, 2004 (gmt 0)

10+ Year Member



Do two things. First, use string interpolation instead of concatenation. But I don't think that's really the problem. It's just more elegant.

$iCursor = $_GET["cursor"];
$url = "index.php?cursor=$iCursor";

Second, debug the url. Comment the header() line and print the url. If it gets printed with cursor=0 you know for sure that the problem is in the code above.

if ($_POST["cancel"]) {
//header("Location: $url");
echo "The url is '$url'";
}

For example, it could be that $_GET["cursor"] isn't 2 but something else, empty for example. Actually, this might be it: $_GET["cursor"] for some reason is the empty string and (int) of the empty string is 0.

coopster

1:04 pm on Dec 1, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, renonet.

Hanu has a good tip here, but I wouldn't even wait to get into the "if" logic statement, dump the value immediately and troubleshoot from there.

$iCursor = $_GET["cursor"]; 
$url = 'index.php?cursor='.(int) $iCursor;
exit [php.net]("The url is '$url'");

renonet

4:15 pm on Dec 1, 2004 (gmt 0)

10+ Year Member



I just don't understand why i can't redirect some variable got from server variable, like $_GET or $_POST, but if I hard-coded the value like this:

$iCursor = 2;

the header can pass the value on, but not if

$iCursor = $_GET["cursor"];

I also echoed $iCursor and the value is there, but it will have been empty when redirected to another page.

Hanu

9:55 am on Dec 2, 2004 (gmt 0)

10+ Year Member



OK, something in $_GET["cursor"] causes it to be cast to an integer zero. It's not the redirect that causes the problem. It's $_GET["cursor"].

Things to check:

- Try interpolation as I suggested in my earlier post and tell us what happens.

- What's your PHP version? Before 4.1.0 you needed to use $HTTP_GET_VARS instead of $_GET.

- Examine $_GET["cursor"] closely. Is it really set? Use

phpinfo( INFO_VARIABLES );

to dump the value of all get, post, cookie and server variables.

- $_GET is an associative array (hash). The key lookup with hashes is case sensitive. $_GET["CURSOR"] is different to $_GET["cursor"] which is different to $_GET["Cursor"].

HTH