Jump to content

*SIGH* Very basic Session help if you would be so kind...


Recommended Posts

After 8 hours of beating my head against whatever was close I have come for help. I searched the forums, but as this is my 3rd day using PHP i am unsure of the proper verbage to use.

 

What I am trying to accomplish is basically a "quiz" for my customers. They enter their first name, last name, and month into a field and that will create a text file that I will then put/grab that data from later. I have completely simplified everything down to just a single variable being stored in the session so I can understand whats going on.

 

My problem comes about when I try to move from page to page keeping the file name constant. My variable, which will not change from any page. This is why I believe I want a $_session. My session is following me to the second page.

 

I have tried the following with server globals on and off. Who will stop my head from bleeding more?

 

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<?php 
session_start();
if (isset($_POST['Submit'])) { 
$_session['UserName'] = $_POST['firstname'];
}
?>
<form action=addressgoeshere.php method="POST">
Your First Name:<INPUT TYPE="TEXT" NAME="firstname" size="35">
<body>
</body>
</html>

 

The second page "addressgoeshere.php"

 

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<?php
session_start();
echo "The username is:";
echo $_session['UserName'];
echo "...";
?>
<body>
</body>
</html>

You need to enable error reporting, and also read the sticky thread at the top of this forum titled, "HEADER ERRORS - READ HERE BEFORE POSTING".

 

Ok, read the header errors... not sure I learned much but as im learning im sure to reference it again.

 

So i enabled error reporting.

 

The username is: Notice: Undefined index: UserName in WEBADDRESSOFHTMLFILEHERE on line 14 ...

 

14.     echo $_SESSION['UserName'];

That's not the only error you're getting, unless you've changed your code after reading the article about header errors?

 

You are correct. I have changed my code in an attempt to follow the direction of the previous help.

 

<html xmlns="http://www.w3.org/1999/xhtml">
<?php 
ob_start();
session_name("ThisSession");
session_start();
ini_set('display_errors',1); 
error_reporting(E_ALL);
$_SESSION['UserName'] = "asd";
if (isset($_POST['Submit'])) { 
$_SESSION['UserName'] = $_POST['firstname'];
$url = 'targetfileishere';
    echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">'; 
}
ob_end_flush();
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form action=addresshere/index.phtml
method="POST">
Your First Name:<INPUT TYPE="TEXT" NAME="firstname" value="firstname" size="35">
<input type="submit" name="Submit" value="Submit">
</body>
</html>

 

Page 2 now

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>
<?php
session_name("ThisSession");
session_start();
ini_set('display_errors',1); 
error_reporting(E_ALL);
echo "The username is:";
echo $_SESSION['UserName'];
echo "...";
?>
</body>
</html>

 

And the Notice given

 

The username is: Notice: Undefined index: UserName in FILELOCATIONHERE on line 17 ...

 

Line 17 -

echo $_SESSION['UserName'];

You're still not doing it correctly, and that notice is not the only message you should be given. I recommend re-reading the HEADER ERRORS article, and also take a look in the PHP manual for header (); The warning in the description is true for session_start () as well, as they both send out HTTP headers.

You should be getting a "headers already sent" notice due to the output being sent to the browser before session_start(). Output buffering is pretty much a hack, and you should really avoid using it. This code is certainly fixable without output buffering. Your error reporting directives in php.ini should be as follows:

error_reporting = -1

display_errors = On

Then after saving the file, restart Apache.

 

Or for the time being, you can add the following at the head of your scripts, right after the opening php tag:

error_reporting(-1);
ini_set('display_errors', 'On');

You're still not doing it correctly, and that notice is not the only message you should be given. I recommend re-reading the HEADER ERRORS article, and also take a look in the PHP manual for header (); The warning in the description is true for session_start () as well, as they both send out HTTP headers.

 

Got it solved. Thank you.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.