Page 1 of 1
Passing a variable
Posted: Wed Mar 08, 2006 7:34 am
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
Posted: Wed Mar 08, 2006 7:41 am
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.
Posted: Wed Mar 08, 2006 8:06 am
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>
Posted: Wed Mar 08, 2006 8:09 am
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?)
Posted: Wed Mar 08, 2006 8:13 am
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'];
Posted: Wed Mar 08, 2006 8:14 am
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

$_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".....
Posted: Wed Mar 08, 2006 8:16 am
by Grim...
Oh yeah

Posted: Wed Mar 08, 2006 9:04 am
by chrislynch
I now get an undefined variable SESSION on the line i call
Posted: Wed Mar 08, 2006 9:08 am
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");
Posted: Wed Mar 08, 2006 9:23 am
by chrislynch
Thats the way i have.... Where should i put the session_start(); code?