Solved

Retrieve session data after page forwarding using the header function

Posted on 2009-03-31
4
683 Views
Last Modified: 2012-05-06
I am trying to have persons register to my website and then have Welcome <name they registered with> on the next page.
I saved formation to a session as follows in the register.php page:
$_SESSION['username']= $username;

I then forwarded the user to the hello.php where they would be greeted with the welcome message as follows:
header('Location:hello.php');

Next, I tried to retrieve information from the session variable in the hello.php as follows:
$username = $_SESSION['username'];
echo $username;

However, the username did not appear. What am I doing wrong and how can I fix it?
See code below (register.php and hello.php).
.........................Hello.php............................

<?php

session_start();
 

$username = $_SESSION['username'];

echo $username;
 

echo 'Registration successful';

session_write_close();

?>
 
 
 

..........................................Register.php...................................................

<html>

<head>

<title>Registration form</title>

</head>
 

<body>
 

<h3>Registration Form</h3>
 

<?php

session_start();

define('SALT_LENGTH', 9);

if(isset($_POST['sendUsername']))

{

    $username = $_POST['username'];

    $password1 = $_POST['password1'];

    $password2 = $_POST['password2'];

    	if(empty($username))

   	{

      	echo "You haven't filled in username!<br/>";

    	}

    	if(empty($password1))

    	{

        	echo "You haven't filled in password!<br/>";

    	}

    	else if(!preg_match("/^[a-zA-Z0-9.,?_]+$/",$password1))

    	{

		echo "You have entered an invalid password!<br/>";

	}

	else if(strlen($password1)<5)

	{

		echo "Your password must be at least 5 characters long!<br/>";

	}

	if(empty($password2))

    	{

        	echo "You haven't retyped your password!<br/>";

     	}

	else if(!preg_match("/^[a-zA-Z0-9.,?_]+$/",$password2))

    	{

		echo "You have entered an invalid password!<br/>";

	}

	else if(strlen($password2)<5)

	{

		echo "Your password must be at least 5 characters long!<br/>";

	}

	else if($password1!=$password2)

	{

		echo "Your password must be the same!<br/>";

	}

	else

	{

		$dbServer = '127.0.0.1';

		$dbUser = 'alpha';

		$dbPass = 'qwerty';

		$dbName = 'myusers';
 

		$conn = mysql_connect($dbServer, $dbUser, $dbPass) or

			die('Error connecting to mysql' . mysql_error());

		$db = mysql_select_db($dbName, $conn) or

			die('Error selecting database' . mysql_error());

		$username = mysql_real_escape_string($username);

		$password = generateHash($password1);

		$query1 = "SELECT * FROM users WHERE username = '$username'";

		$result = mysql_query($query1);

		if (mysql_fetch_assoc($result)!=0)

		{

			echo "The user name entered is already taken";

		}

		else 

		{

			$query = "INSERT INTO users(username, passwordHash) VALUES('$username','$password')";

			//mysql_query($query);

			$_SESSION['username']= $username;

			header('Location:hello.php');

		}

		

		

		mysql_close($conn);

	}

}
 
 

function generateHash($plainText, $salt = null)

{

    if ($salt === null)

    {

        $salt = substr(md5(uniqid(rand(), true)), 0, SALT_LENGTH);

    }

    else

    {

        $salt = substr($salt, 0, SALT_LENGTH);

    }
 

    return $salt . sha1($salt . $plainText);

}

session_write_close();

?>
 

<p>
 

<form method="post" action="register.php">

<b>Username:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b><input name="username" type="text" size="20"  value="<?php echo $username; ?>"><br/>

<b>Password:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b><input name="password1" type="password" size="20"  value="<?php echo $password1; ?>"><br/>

<b>Retype password:</b><input name="password2" type="password" size="20"  value="<?php echo $password2; ?>"><p>

<input name="sendUsername" type="submit" value="submit">

</form>
 

</body>

</html>

Open in new window

0
Comment
Question by:kgpretty
  • 2
4 Comments
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 125 total points
ID: 24035053
In Register.php, you must call session_start() before you send any output to the browser. Start it like this:

<?php session_start(); ?>
<html>
<head>
<title>Registration form</title>
</head>
0
 

Author Comment

by:kgpretty
ID: 24035839
Where should lines 26 - 111 go?
0
 
LVL 2

Expert Comment

by:toymachiner62
ID: 24035858
I think they'll be alright. Just try putting line 25 at the very beginning
0
 

Author Closing Comment

by:kgpretty
ID: 31565124
Thanks
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
advertisement module in core php 4 92
compressing images after upload 4 31
.htaccess file settings 4 36
php image upload 3 27
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now