PHP variable in header function
Asked Answered
H

6

5

I am attempting to pass variables through the URL using the header function as a way to redirect the page. But when the page is redirected it passes the actual variable names rather than the values associated with the variables. I am new to PHP and do not fully understand the syntax so any further explanation as to the proper way to do this would be much appreciated.

header('location: index.php?id=".$_POST[ac_id]."&err=".$login."');
Hanoverian answered 26/4, 2011 at 2:30 Comment(0)
A
17

You want:

header("Location: index.php?id=".$_POST['ac_id']."&err=".$login);

You were combining ' and " in this string, which is why it couldn't interpolate the variables properly. In my example above, you are strictly opening the string with " and concatenating the variables with the string.

Ashmore answered 26/4, 2011 at 2:32 Comment(5)
Both lost quotes in $_POST ;-)Tankard
this will raise a notice because of the unquoted string for the array key.Olympiaolympiad
there is no reason to concatenate the strings & variables, as PHP will expand variables inside double quoted stringsOlympiaolympiad
It is personal preference, and if you want to be nit picking about micro-performance see here(concatenation is slightly faster): #14120Ashmore
It's generally accepted by most developers I've worked with that if you want to do string concatenation, use single quotes, if you want to use inline variable expansion, use double quotes.Olympiaolympiad
S
3

You have quotes within quotes. Try this instead:

header('location: index.php?id=' . urlencode($_POST['ac_id']) . '&err=' . urlencode($login));

The urlencode() function takes care of any reserved characters in the url.

What I would do instead is use http_build_query(), if you think you will have more than one or two variables in the URL.

header('Location: index.php?' . http_build_query(array(
    'id' => $_POST['ac_id'],
    'err' => $login
)));

Also, you technically can't use relative paths in the location header. While it does work with most browsers, it is not valid according to the RFCs. You should include the full URL.

Sopor answered 26/4, 2011 at 2:33 Comment(1)
Here too... No quotes for $_POST key nameTankard
D
1

Try SESSION storage. header is use to redirect the page. and if you really want to pass values through header only then u have genrate url. header('location:destination.php? value1=1&value2=3'); but it is not a good practice for vars. just store values in SESSION global var. B4 the header() redirection call. @the recieve page u have to test, if the session val isset() n !empty() then ... or else ...

Hope this wil help.

Dorettadorette answered 6/2, 2014 at 11:41 Comment(0)
B
-1

In my case, many times header() not working properly. Instead of header function, I use window.location.href. You can try like this,

echo "<script> 
            window.location.href='index.php?id=".$_POST[ac_id]."&err=".$login';
      </script>";
Brigitta answered 29/1, 2021 at 18:8 Comment(0)
V
-3
header('location: index.php?id='.$_POST['ac_id'].'&err='.$login);
Vanatta answered 26/4, 2011 at 2:32 Comment(1)
Both lost quotes in $_POST ;-)Tankard
O
-3

Try this:

header("location: index.php?id=$_POST[ac_id]&err=$login");

PHP variables are expanded inside double-quoted strings.

Olympiaolympiad answered 26/4, 2011 at 2:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.