Jump to content

Can't log in, just refreshing the page


budder

Recommended Posts

Hey PhpFreaks (:

Got a problem with my log in script. When I type the username and password it only returns the log in page.

I can't find the error and what causing the script not to log in.

It returns the first login form, not the second as it should if the pass or user is wrong.

<?
session_start(); // start session.
?>
<!-- header tags, edit to match your own, or include template header file. -->
<html>
<head>

<title>Login</title>
<head>
<body>
<?
if(!isset($username) | !isset($password)) {
// escape from php mode.
?>

<form action="<?=$PHP_SELF?><?if($QUERY_STRING){ echo"?". $QUERY_STRING;}?>" method="POST">
<p align="center">Members only. Please login to access this document.</p>
<table align="center" border="0">
<tr>
  <th>
Username:
  </th>
  <th>
<input type="text" name="username">
  </th>
</tr>
<tr>
  <th>
Password:
  </th>
  <th>
<input type="password" name="password">
  </th>
</tr>
<tr>
  <th colspan="2" align="right">
<input type="submit" value="Login">
</form>
  </th>
</tr>
</table>
</body>
</html>
<?
exit();
}

// If all is well so far.

session_register("username");
session_register("password"); // register username and password as session variables.

$dbHost		=	"sql01.dk";
$dbUser		=	"elmerdahldk";
$dbPass		=	"rNnj5Tc2";
$dbDatabase	=	"elmerdahldk";

$mysqli = mysql_connect("$dbHost", "$dbUser", "$dbPass");
mysql_select_db($dbDatabase, $mysqli) or die("Can not connect");
// Check connection
if (mysql_error()) {
	printf("Unable to connect to database: %s", mysql_error());
	exit();
}

$sql = mysql_query("SELECT password FROM users WHERE username = '$username'");
$fetch_em = mysql_fetch_array($sql);
$numrows = mysql_num_rows($sql);

if($numrows != "0" & $password == $fetch_em["password"]) {
$valid_user = 1;

}
else {
$valid_user = 0;
}

// If the username exists and pass is correct, don't pop up the login code again.
// If info can't be found or verified....

if (!($valid_user))
{
session_unset();   // Unset session variables.
session_destroy(); // End Session we created earlier.
// escape from php mode.
?>

<form action="<?=$PHP_SELF?><?if($QUERY_STRING){ echo"?". $QUERY_STRING;}?>" method="POST">
<p> align="center"><b>Forkert brugernavn eller kode</b></p>
<p align="center">Incorrect login information, please try again. You must login to access this document.</p>
<table align="center" border="0">
<tr>
  <th>
Username:
  </th>
  <th>
<input type="text" name="username">
  </th>
</tr>
<tr>
  <th>
Password:
  </th>
  <th>
<input type="password" name="password">
  </th>
</tr>
<tr>
  <th colspan="2" align="right">
<input type="submit" value="Login">
</form>
  </th>
</tr>
</table>
</body>
</html>
<?
exit();
}
?>

 

Link to comment
https://forums.phpfreaks.com/topic/198481-cant-log-in-just-refreshing-the-page/
Share on other sites

try this

 

<?
error_reporting(0);
session_start(); // start session.
?>
<!-- header tags, edit to match your own, or include template header file. -->
<html>
<head>
<title>Login</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<p align="center">Members only. Please login to access this document.</p>
<table align="center" border="0">
<tr>
  <th>
Username:
  </th>
  <th>
<input type="text" name="username">
  </th>
</tr>
<tr>
  <th>
Password:
  </th>
  <th>
<input type="password" name="password">
  </th>
</tr>
<tr>
  <th colspan="2" align="right">
<input type="submit" name="submit" value="Login">

  </th>
</tr>
</table>
</form>
</body>
</html>

<?
// If all is well so far.
if(isset($_POST['submit']))
{
echo "Hai";    
$username=$_POST['username'];
$password=$_POST['password'];
session_register("username");
session_register("password"); // register username and password as session variables.

$dbHost = "sql01.dk";
$dbUser = "elmerdahldk";
$dbPass = "rNnj5Tc2";
$dbDatabase = "elmerdahldk";
$mysqli = mysql_connect("$dbHost", "$dbUser", "$dbPass");
mysql_select_db($dbDatabase, $mysqli) or die("Can not connect");
// Check connection
if (mysql_error()) {
printf("Unable to connect to database: %s", mysql_error());
exit();
}
$sql = mysql_query("SELECT * FROM users WHERE username = '$username' and password='$password'");
//$fetch_em = mysql_fetch_array($sql);
$numrows = mysql_num_rows($sql);
if($numrows != "0")
{
    $valid_user = 1;
}
else 
{
    $valid_user = 0;
}
// If the username exists and pass is correct, don't pop up the login code again.
// If info can't be found or verified....
if ($valid_user == 0)
{
echo " sorry wrong details";
session_unset();   // Unset session variables.
session_destroy(); // End Session we created earlier.
// escape from php mode.
}
else
{
    echo "Success";
}
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.