views:

67

answers:

3

I have actually discovered my problem but I am really want to know why this is an issue. I had two pages form1.php I started a session on that page and hit submit. Then I had a link to session2.php which started that session and was able to pull the information from form1.php. I am just learning about sessions and this was a very simple exercise to learn what a session can do.

Here lies the issue, I had a stylesheet link in my head and it had a blank href well it was href="#" and when that was there the session2.php would not start the session from form1.php and grab the info from the form. Without that href="#" in the style tag it worked fine, and it also worked fine if it was a fake styletag href="something.css" but href="" doesn't work either.

Why is this? I only have those in because its a template I made for workflow, maybe I cant include the css link in my template anymore to prevent future issues.

You can see this site working here, if I haven't explained myself.

form1.php

<?php
session_start();
$_SESSION['name'] = $username;
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html>

<head>
<title></title>
<!--//CSS STYLESHEETS//-->
<link rel="stylesheet" href="#" type="text/css">
</head>

<body>
<a href="sessions2.php">Go to session 2</a>
<!--form stuff is in here-->
</body

session2.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html>

<head>
<title></title>
</head>

<body>
   <?php
 session_start();
  $username = $_SESSION['name'];
  echo $username;


  ?>
  </body>
</html>
A: 

Are you trying to output stuff to the page before you send the headers? What happens if you put the stylesheet after you call session_start()?

easement
for form.php the stylesheet is after the session_start
Anders Kitson
+1  A: 

To work like you want it, you need to have started the session first. Sounds simple, because it is. When you say session_start, php then looks for an accepted session cookie first to process content.

From http://php.net/manual/en/function.session-start.php

Note: To use cookie-based sessions, session_start() must be called before outputing anything to the browser.

Liam
I posted my code above, session_start for form.php is first.
Anders Kitson
but not in session2
Liam
+1  A: 

Your second page needs to look like this:

<?php
    session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html>
    <head>
       <title></title>
    </head>
    <body>
    <?php
        $username = $_SESSION['name'];
        echo $username;
    ?>
    </body>
</html>

Note that session_start() must appear before any content is printed to the screen.

Per the note on the session_start PHP manual page:

Note: To use cookie-based sessions, session_start() must be called before outputing anything to the browser.

Mark E
ok I will do this, will this relate to why I cant have the blank href in form.php the first one.
Anders Kitson
it's unclear if `<link rel="stylesheet" href="#">` is valid syntax even for HTML, but you're telling the browser it's XHTML, and not closing the tag, which could be causing issues. Try `<link rel="stylesheet" href="#" />` instead (note that slash)
Mark E