tags:

views:

82

answers:

5

I'm looking at this code and thinking I can't see the error because there is somethign wrong but I just can't see it... I'm hoping someone here can see it as staring at it for the last half an hour hasn't made it clear to me.

The code:

if (!empty($_SESSION['email_notifications'])) { 

  print '<br>SESSION[email_notifications] = ['.$_SESSION['email_notifications'].']';
  print '<br>Session exists!!!';

  $from = $_SESSION['display_name'].' <'.$_SESSION['email_notifications'].'>';
  print '<br>$from = ['.$from.']';

}

Outputs this:

SESSION[email_notifications] = [[email protected]]

Session exists!!!

$from = [ ]

The session value is set but when it is stored in a variable it disappears???

+7  A: 

You are outputting that to HTML, right? Remember &lt; and &gt;? Check out the page source if it's there. In all likelihood, the email address portion is being interpreted as an unknown HTML tag, and therefore ignored.

As to why your display_name isn't displayed, I don't know. Does it have a value?

Amadan
Nice catch. That's probably because that one's not in the session.
Artefacto
+3  A: 

Hmmmm. Php5?

This is crazy silly, but have you checked the output SOURCE, rather than html? You've put angle brackets around it, it may just not be rendering on the screen.

Satanicpuppy
A: 

Since the <br> tags do not show up in the output, I'm guessing that you've copy-pasted this directly from what the browser shows. The browser will not display HTML tags, but uses them to format the real text, or ignore them if it doesn't "know" what to do with a tag. If you want to see the real output, try the "display page source" option of your browser.

That being said, in the second case the email address is outputted with '<' and '>', so it appears as a tag to the browser. The displayname variable is probably not set in the session, or it is a string that has '<' and '>' around it too.

catchmeifyoutry
+2  A: 

If you want to output sanatised HTML you can use some of the built in functions PHP offers to make it output to the browser correctly. Using your code, try the following:

if (!empty($_SESSION['email_notifications'])) { 

  print '<br>SESSION[email_notifications] = ['.$_SESSION['email_notifications'].']';
  print '<br>Session exists!!!';

  $from = $_SESSION['display_name'].' <'.$_SESSION['email_notifications'].'>';
  print '<br>$from = ['.htmlentities($from).']';

}
Sam152
A: 

Ok... I found out the issue...

$_SESSION['display_name'] was empty which was throwing me...

By encapsulating $_SESSION['email_notifications'] in < > html acts like a mail program and was hiding the email address behind the preceeding display_name and because the preceeding display_name was empty, it was just being hidden altogether...

php-b-grader
HTML does not "act as an email program". If a browser sees <p>, it gets interpreted as "new paragraph". If a browser sees <strong>, it gets interpreted as "make this bit boldish". If a browser sees <foo>, it gets confused and just ignores it. *That's* what's happening to your brackets - it being an email address has nothing to do with it.
Amadan
Thanks Amadan for taking my statement so literally... I know it doesn't work like an email program! I was suggesting moreso that interpretation of the < > was occuring... mail program was an incorrect explanation
php-b-grader
You're welcome. Being literal is what programmers do best.
Amadan