Passing a variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chrislynch
Forum Newbie
Posts: 9
Joined: Tue Mar 07, 2006 9:44 am

Passing a variable

Post by chrislynch »

HI,

I have two little questions

1:)

I have a login form and when the user logs in successfully name and password match I send them to another page....
On this page it just says welcome back "user_name".....

I want to pass the user_name to the next page.

I have this code in the welcome.php page which is opened using the header function

Code: Select all

Welcome back<?php echo $_POST["user_name"]; ?>.
I get this error
Notice: Undefined index: user_name in c:\program files\easyphp1-8\www\welcome.php on line 9

How can i pass the user name from one page to the next?

Thanks
Chris Lynch
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

On the form, have you used action="post" or action="get"?

Debug tip - add the following code to your page

Code: Select all

print "<pre>";
print_r($_POST);
print "</pre>";
This will show all the variables that have been posted to that page, and their values.
chrislynch
Forum Newbie
Posts: 9
Joined: Tue Mar 07, 2006 9:44 am

Post by chrislynch »

this is the code for the login form

Code: Select all

<?php
 
 //Variables used to access the database
 $hostname = "localhost";
 $database = "access_point_users";
 $username = "**********";
 $password = "**********";
 
//connection to the database
$ttt = mysql_connect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR);

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) 
	  {
   		$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
		}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "users")) 
{
 	
	//Selects the user_name with the entered password
	$sql = "SELECT Password FROM users WHERE User_Name = '$_POST[user_name]'";
 	mysql_select_db("access_point_users", $ttt) or die( "Unable to select the database");
  $Result1 = mysql_query($sql) or die(mysql_error());
	
	//Retrieves the password which matches the user_name
	while ($row = mysql_fetch_array($Result1, MYSQL_BOTH)) 
	{
  	//Compares the entered password with the retrieved user name
	  if($row[0]== $_POST["password"])
	  {	    
			 header("Location: ./welcome.php");                                    
	  }
		else
		{		 	 
			header("Location: ./error.php");			 
		}
  }
}
?>
<html>
<head>
<title>Access Point Login</title>
</head>
<body>
<h1>
<font color="red"><em><b>Login to use the Access Point</b></em></font>
</h1>
This access point is provided free of charge by UCC, all you need
is a wireless enabled device and off you go!
<br /><br />
<form name="users"method ="POST"action ="<?php echo $editFormAction;?>">
<address><label>User Name:</label></address>
<input type="text" name="user_name" value="" />
<br /><br />
<address><label>Password:</label></address>
<input type="password" name="password" value="" />
<br />
<br />
<input type="submit" name="submit" value="Login">
<br />
<br /><i>If you do not have an account please 
<a href="/register.php"><span style="color:blue">Register.</span></a>
</i><hr /></i><hr /><input type="hidden" name="MM_insert" value="users"></form>
If you have any trouble with login/registration please contact the 
<A HREF="mailto:cl8@student.cs.ucc.ie">Administrator</A> with the 
exact title <i><b>Access Point login/registration. </b></i><br />
<font size="1">We can not be held responsible for any damaging programs/viruses etc
that you may download while using this access point. We recommend that
you have up-to-date anti-virus software running on your machine</font>
</body>
</html>
this is the code for the welcome form

Code: Select all

<html>
<head>
<title>Access Point Login Successful</title>
</head>
<body>
<h1>
<font color="red"><em><b>Welcome</b></em></font>
</h1>
Welcome back<?php echo $_POST["user_name"]; ?>.
<br /><br />
</i><hr /></i><hr />
If you have any trouble with login/registration please contact the 
<A HREF="mailto:cl8@student.cs.ucc.ie">Administrator</A> with the 
exact title <i><b>Access Point login/registration. </b></i><br />
<font size="1">We can not be held responsible for any damaging programs/viruses etc
that you may download while using this access point. We recommend that
you have up-to-date anti-virus software running on your machine</font>
</body>
</html>
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Code: Select all

<form name="users"method ="POST"action =" echo $editFormAction;?>">
Some funny spaces in there.
Change it to

Code: Select all

<form name="users" method="POST" action ="echo $editFormAction;?>">
and try again.

(I'm not sure it will make a difference, though. Did you try the print_r($_POST); thing? If so, what did it spit out?)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

(edit your post so it doesn't show your password)

There are several ways you can store the users name to display on different pages.

On the login page do this...

Code: Select all

session_start();

## validate your login here

$_SESSION['username'] = $_POST['username']

## now, wherever you want to call the username,
## just put echo $_SESSION['username'];
or

Code: Select all

## validate your login
setcookie("username",$_POST['username'],time()+60*60*24*30);

## now you can call the username by using echo $_COOKIE['username'];
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Grim... wrote: (I'm not sure it will make a difference, though. Did you try the print_r($_POST); thing? If so, what did it spit out?)
He's redirecting them to another page :P $_POST will no longer be set
I have a login form and when the user logs in successfully name and password match I send them to another page....
On this page it just says welcome back "user_name".....
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Oh yeah :)
chrislynch
Forum Newbie
Posts: 9
Joined: Tue Mar 07, 2006 9:44 am

Post by chrislynch »

I now get an undefined variable SESSION on the line i call

Code: Select all

echo $_SESSION['username'];
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

You need to put

Code: Select all

$_SESSION['username'] = $_POST['username']
on the line just above

Code: Select all

header("Location: ./welcome.php");
chrislynch
Forum Newbie
Posts: 9
Joined: Tue Mar 07, 2006 9:44 am

Post by chrislynch »

Thats the way i have.... Where should i put the session_start(); code?
Post Reply