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'] . ';">';
Sign up to request clarification or add additional context in comments.

1 Comment

+1 Suggest also adding in htmlentities() or htmlspecialchars() to stop unexpected values from breaking the HTML output.
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']);

Comments

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

Comments

0

Try this

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

or use

header("Location: http://www.example.com/" . $_SESSION['name']);

Comments

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

Should work fine.

Comments

0

Maybe like this:

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

Comments

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.