PHP variable in header function

Joe picture Joe · Apr 26, 2011 · Viewed 63.7k times · Source

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."');

Answer

Mike Lewis picture Mike Lewis · Apr 26, 2011

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.