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
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";
}
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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