Forum Moderators: coopster

Message Too Old, No Replies

append variable to url: loop!

         

rufuz

9:46 pm on Jan 11, 2009 (gmt 0)

10+ Year Member



First off, I'm new here and at php.

I'd like to append a variable to all my urls:

<?php
$url = $_SERVER['REQUEST_HEADER'];
$finalurl = $url.'?var=1';
if ($url != $finalurl) {
header("Location:$finalurl");
} else {
echo "solved!";
}
?>

it appends the ?var=1 but also gives a redirect loop.

I tried also with REQUEST_URI and same result...

it seems the condition I put is ignored...

any help is apreciated...

thanks!

ricc.

PokeTech

11:20 pm on Jan 11, 2009 (gmt 0)

10+ Year Member



<?php
$var = $_GET['var']; //Gets the var from the url (if exists)

$new_var = "1"; //If $var is not set than what do you want $var to say
$path = $_SERVER['REQUEST_URI'] . "?var=" . $new_var;

if (!isset($var)) {
header("Location: $path");
} elseif ($var != $new_var) {
$path = $_SERVER['REQUEST_URI'] . "&var=" . $new_var;
header("Location: $path");
}
?>

Little longer but it should work.

rufuz

1:56 am on Jan 12, 2009 (gmt 0)

10+ Year Member



It's nice to see when computers don't understand you, humans still can!

Thanks a milion PokeTech, it works.

rufuz

3:51 am on Jan 12, 2009 (gmt 0)

10+ Year Member



ok, a little step ahead...

now I'm trying to put myself $new_var in the url instead having it already assigned.

Then I'd like the script (that's included in a template, I have it in every page) to retain that value until I put a new value.

e.g. when I write in adress bar:

www.mypath?var3

it should be retained for the next pages. Until I write:

www.mypath?var4

That also should be retained, and so on.

But I'm a bit lost...

If you can help again, Thanx!

ricc.

rufuz

7:06 am on Jan 12, 2009 (gmt 0)

10+ Year Member



Finally I find this solution...

<?php
$var = $_GET['var']; //Gets the var from the url (if exists)
setcookie("TestCookie", $var, time()+3600);
$new_var = "1"; //If $var is not set than what do you want $var to say (default value)
$new_var = $_COOKIE['TestCookie'];
$path = $_SERVER['REQUEST_URI'] . "?lang=" . $new_var;
if (!isset($var)) {
if ($var != $new_var) {
header("Location: $path");}
}
?>

It works, but unfortunately it relies on cookies... no idea if there are alternative solutions though... (session?)

bye

ricc.