Del
Del

Reputation: 13

PHP simple session issue with an array

I have an array session with emails - I try to echo them to the screen and I get nothing appearing.

$_SESSION['extralistids'] = array('<[email protected]>', '<[email protected]>', '<[email protected]>', '<[email protected]>', '<[email protected]>');
echo 'You have visited this page ' . $_SESSION['extralistids'][$_SESSION['counter']] . ' -for the first time.';

I use this and it outputs fine:

$_SESSION['extralistids'] = array('abc', 'def', 'ghi', 'jkl', 'def', 'ghi', 'jkl', 'def', 'ghi', 'jkl', 'def', 'ghi', 'jkl');

I've tried replacing single quotes with double quotes - no joy. I'm doing something obviously wrong here but can't seem to spot the mistake - any thoughts where I am going wrong?

Upvotes: 1

Views: 42

Answers (2)

Barmar
Barmar

Reputation: 780655

Assuming you're displaying this on a web page, you need to do:

echo 'You have visited this page ' . htmlentities($_SESSION['extralistids'][$_SESSION['counter']]) . ' -for the first time.';

because < and > have special meaning in HTML. The browser is treating <[email protected]> as an HTML tag, not text to display. htmlentities will translate that to &lt;[email protected]&gt;, and then it will display as intended.

Upvotes: 1

John Conde
John Conde

Reputation: 219794

Nothing appears because they are wrapped in brackets (< and >.) which makes them look like HTML. You need to use htmlentities() when echoing out those values so the brackets are displayed as entities and not interpreted as HTML characters.

echo 'You have visited this page ' . htmlentities($_SESSION['extralistids'][$_SESSION['counter']]) . ' -for the first time.';

Upvotes: 1

Related Questions