View Full Version : PHP Goto page if Password correct
doog
January 20th, 2005, 08:44 PM
Okay, I am new to php and this is probably a dumb question, but I want to go to a different page when using the <form action =<?echo $PHP_SELF?>>
PHP Code Sample - Capital letter are used in place of code where I am having the issues. Thanks,
if ($submit) {
$db = MYSQL_CONNECT($server, $user, $DBPassword) or die ( "<H3>Server unreachable</H3>");
if(! $db){
die("Could not connect to MySQL");
}
else {
mysql_select_db("dbname",$db) or die ("cant change");
$result=mysql_query("select * from tbl_UserInformation where Email='$txtUserName'",$db) or die ("cant do it");
while ($row=mysql_fetch_array($result)) {
if ($row["Password"]==$txtPassword) {
START SESSION
GOTO --> GeneralInformationPage.php;
}
else {
CALL THIS PAGE AGAIN
}
}}}
degsy
January 20th, 2005, 09:55 PM
You could do it like this
<?
if (!$Submit) {
// If the Submit button wasn't clicked then show the login form
?>
<form name="form1" id="form1" method="post" action="<? $PHP_SELF; ?>">
<p> Email:
<input name="txtUserName" type="text" id="txtUserName">
<br>
Pass:
<input name="txtPassword" type="text" id="txtPassword" />
</p>
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
</form>
<?
}
// If the form was submitted
else {
// start the connection to the database
$server = 'dbserver';
$user = 'dbuser';
$DBPassword = 'dbpass';
$dbname = 'dbname';
$db = MYSQL_CONNECT($server, $user, $DBPassword) or die ( "<H3>Server unreachable</H3>");
if(! $db){
die("Could not connect to MySQL");
}
else {
mysql_select_db("$dbname",$db) or die ("cant change");
$result=mysql_query("select * from tbl_UserInformation where Email='$txtUserName' AND Password='$txtPassword'",$db) or die ("cant do it");
// If no match was found show the login form
if (mysql_num_rows($result) != 1 ) {
Header("Location: $PHP_SELF");
}
else {
// if a match was found then redirect to another page
while ($row=mysql_fetch_array($result)) {
if ($row["Password"]==$txtPassword) {
Header("location: http://www.google.com");
}
}
}
}
}
?>
In future you would want to put more error correction into the code, such as
Error: No Username found
Error: Wrong Password
etc.
I added a bit of code, just to get it working
Code to Match Email & Password
Email='$txtUserName' AND Password='$txtPassword'
Code to Check if Email is found
if (mysql_num_rows($result) != 1 ) {
Header("Location: $PHP_SELF");
}
doog
January 21st, 2005, 09:04 PM
The Code 'Header("location: http://www.google.com");' (http://www.google.com"<img%20src="images/smilies/wink.gif"%20border="0"%20alt=""%20title="Wink"%20s milieid="44"%20class="inlineimg"%20/>;') does not open www.google.com (http://www.google.com).
Do you code all the pages within one page? For instance, the logIn and User Information are stored within the same php page but displayed at different times on two different pages depending on the user's actions?
degsy
January 21st, 2005, 09:10 PM
It works for me.
What is it doing for you?
doog
January 22nd, 2005, 10:08 PM
The web address does not change, but the text fields disappear. I tried putting a printf statement after the Header("location: http://www.google.com"); and the code prints what ever I write, so I know the password and username are executing correctly. Why is the code not redirecting me to a different page?
degsy
January 22nd, 2005, 11:31 PM
Could you post your code?
Also what System are you running the script on.
doog
January 24th, 2005, 08:44 PM
Thanks for taking the time to look at this degsy.
PHP Version 4.3.10
Apache/1.3.29
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>LogIn Page</title>
</head>
<body>
<?php
if (!$submit) {
?>
<form action= "<?php echo $PHP_SELF; ?>" method="post" name="frmLogIn" id="frmLogIn">
<table width="200" border="1">
<tr>
<td> </td>
</tr>
<tr>
<td><table width="200" border="1">
<tr>
<td>UserName</td>
<td><input name="txtUserName" type="text" id="txtUserName"></td>
</tr>
</table></td>
</tr>
<tr>
<td><table width="227" border="1">
<tr>
<td width="67">Password</td>
<td width="144"><input name="txtPassword" type="password" id="txtPassword"></td>
</tr>
</table></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Log In"></td>
</tr>
</table>
</form>
<?
}
else
{
//Removed for code sharing
$server= "";
$user= "";
$DBPassword= "";
$DBName="";
$db = MYSQL_CONNECT($server, $user, $DBPassword) or die ( "<H3>Server unreachable</H3>");
if(! $db){
die("Could not connect to MySQL");
}
else {
mysql_select_db($DBName,$db) or die ("cant change");
$result=mysql_query("select * from tbl_UserInformation where Email='$txtUserName'",$db) or die ("cant do it");
while ($row=mysql_fetch_array($result)) {
if ($row["Password"]==$txtPassword) {
//session_start();
//session_register("txtUserName","txtPassword");
//print("<p>Got here Connect to MySQL server</P>");
Header("Location: http://www.chicagowebrent.com/GeneralInformationPage.php");
}
else{
Header("$PHP_SELF");
}
}
}
}
?>
</body>
</html>
degsy
January 24th, 2005, 09:40 PM
Are you getting a Cannot Modify Header error?
If so you need to put the HTML within the PHP
so move the if statement to the top of the page before opening <!DOCTYPE
and move the else to after the </html>
e.g.
<?php
if (!$submit) {
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>LogIn Page</title>
</head>
<body>
<form action= "<?php echo $PHP_SELF; ?>" method="post" name="frmLogIn" id="frmLogIn">
<table width="200" border="1">
<tr>
<td> </td>
</tr>
<tr>
<td><table width="200" border="1">
<tr>
<td>UserName</td>
<td><input name="txtUserName" type="text" id="txtUserName"></td>
</tr>
</table></td>
</tr>
<tr>
<td><table width="227" border="1">
<tr>
<td width="67">Password</td>
<td width="144"><input name="txtPassword" type="password" id="txtPassword"></td>
</tr>
</table></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Log In"></td>
</tr>
</table>
</form>
</body>
</html>
<?
}
else
{
//Removed for code sharing
$server= "";
$user= "";
$DBPassword= "";
$DBName="";
$db = MYSQL_CONNECT($server, $user, $DBPassword) or die ( "<H3>Server unreachable</H3>");
if(! $db){
die("Could not connect to MySQL");
}
else {
mysql_select_db($DBName,$db) or die ("cant change");
$result=mysql_query("select * from tbl_UserInformation where Email='$txtUserName'",$db) or die ("cant do it");
while ($row=mysql_fetch_array($result)) {
if ($row["Password"]==$txtPassword) {
//session_start();
//session_register("txtUserName","txtPassword");
//print("<p>Got here Connect to MySQL server</P>");
Header("Location: http://www.chicagowebrent.com/Gener...rmationPage.php");
}
else{
Header("$PHP_SELF");
}
}
}
}
?>
doog
January 24th, 2005, 10:42 PM
That's it! Thanks,
The page did not give me an error, but appearly that was the problem.
degsy
January 24th, 2005, 11:18 PM
You may want to check the error_reporting setup in your PHP.INI file