PHP variable in header function

强颜欢笑 提交于 2019-12-05 10:49:44

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.

Rushit

You can try this

header("Location:abc.html?id=".$_POST['id']."id_2=".$var['id_2']);

If it works let me know. This is just an example.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!