0

I'm trying to add a php session into the content as shown below, but it's just showing an empty page. I'm sure the escaping is not correct.

echo "<meta http-equiv=\"refresh\" content=\"0;URL=$_SESSION['name'];\">";

Here's what I'm trying to achieve.

$_SESSION['name'] = 'somepage.php'

And I want to add the session into the url so it redirects to 'somepage'.

7 Answers 7

6

The best way to combine html quotes and variables is to do it in following way:

echo '<meta http-equiv="refresh" content="0;URL=' . $_SESSION['name'] . ';">';
1
  • +1 Suggest also adding in htmlentities() or htmlspecialchars() to stop unexpected values from breaking the HTML output.
    – Treffynnon
    Feb 7, 2012 at 13:41
1

You may want to learn how variables are parsed inside PHP strings:

// do not use single quotes
echo "<meta http-equiv=\"refresh\" content=\"0;URL=$_SESSION[name]\">";

// curly brackets syntax works too but requires quotes; useful in cases when
// variable name is immediately followed by alpha-numeric/underscore character(s)
echo "<meta http-equiv=\"refresh\" content=\"0;URL={$_SESSION['name']}\">";

// curly brackets alternate syntax
echo "<meta http-equiv=\"refresh\" content=\"0;URL=${_SESSION['name']}\">";

Personally, I like using sprintf():

echo sprintf("<meta http-equiv=\"refresh\" content=\"0;URL=%s\">", $_SESSION['name']);
0

inside of a double quoted "smart" string you cannot have a referenced array inside only single variables.

Try This:

echo "<meta http-equiv=\"refresh\" content=\"0;URL=".$_SESSION['name'].";\">";
0

Try this

echo '<meta http-equiv="refresh" content="0;URL=' . $_SESSION['name'] . ';">';

or use

header("Location: http://www.example.com/" . $_SESSION['name']);
0
echo "<meta http-equiv=\"refresh\" content=\"0;URL=".$_SESSION['name'].";\">";

Should work fine.

0

Maybe like this:

echo "<meta http-equiv=\"refresh\" content=\"0;URL=" . $_SESSION['name'] . ";\">";
0

I wonder why noone mentioned (s)printf

printf('<meta http-equiv="refresh" content="0;URL=%s;">', $_SESSION['name']);

This gets very useful once you need to output many variables in text. See the manual.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.