开发者

PHP variable in header function

开发者 https://www.devze.com 2023-02-28 21:32 出处:网络
I am attempting to 开发者_如何学Pythonpass 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

I am attempting to 开发者_如何学Pythonpass 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."');


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.


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.


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.


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>";


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


Try this:

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

PHP variables are expanded inside double-quoted strings.

0

精彩评论

暂无评论...
验证码 换一张
取 消