Jump to content

[SOLVED] Login Help


BlackWidow

Recommended Posts

I have having problems with the following code, sometimes it will login and carry the session to the next page, sometimes it logs in but doesnt carry the session to the next page.  Can anyone see why?  It has me flumuxed.

<?php
//$uname = "";
//$pword = "";
//$errorMessage = "";
//==========================================
//	ESCAPE DANGEROUS SQL CHARACTERS
//==========================================
function quote_smart($value, $handle) {

   if (get_magic_quotes_gpc()) {
       $value = stripslashes($value);
   }

   if (!is_numeric($value)) {
       $value = "'" . mysql_real_escape_string($value, $handle) . "'";
   }
   return $value;
}

if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$uname = $_POST['username'];
$pword = $_POST['password'];

$uname = htmlspecialchars($uname);
$pword = htmlspecialchars($pword);

//==========================================
//	CONNECT TO THE LOCAL DATABASE
//==========================================
$hostname = "removed";
	$username = "removed";
  	$password = "removed";
  	$database = "removed";

$db_handle = mysql_connect("$hostname", "$username", "$password");
$db_found = mysql_select_db("$database", $db_handle);

if ($db_found) {

	//$uname = quote_smart($uname, $db_handle);
	//$pword = quote_smart($pword, $db_handle);

	$sql = "SELECT * FROM user 
                                 WHERE uname = '$uname' 
                                 AND pword = '$pword'";

	$result = mysql_query($sql, $db_handle);

	if ($result) {
		$num_rows = mysql_num_rows($result);

		if ($num_rows > 0) {

			//$db_field = mysql_fetch_assoc($result);
			//$uname = $db_field['uname'];
			session_start();
			$_SESSION['uname'] = $uname;
			$_SESSION['pword'] = $pword;

			header ("Location: loggon.php");

		}else {

			$errorMessage = "Invalid Login.";

			session_start();
			$_SESSION['uname'] = '';
			$_SESSION['pword'] = '';

			//==========================================
			//	YOUR SIGNUP PAGE HERE
			//==========================================
			header ("Location: test_reg.php");
		}	
	}
	else {
		$errorMessage = "Error logging on - no results";
                        //header ("Location: reg.php");
	}


}

else {
	$errorMessage = "Error logging on - last error";
}

}


?>

<?php include("member_header.php"); ?>
<?php include("member_menu.php"); ?> 


<div id="content1">


<form name ="form1" method ="post" action ="logv1.php">
<fieldset>
Username: <input type = "text" name ="username"  value="<?php print $uname;?>" maxlength="20"><br /><br />
Password:   <input type = "text" name ="password"  value="<?php print $pword;?>" maxlength="16"><br />

<p>
<input type = "submit" name = "submit"  value = "Login">
</p>
</fieldset>
</form>

<?php print $errorMessage;?>

</div>

</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/157093-solved-login-help/
Share on other sites

   $uname = htmlspecialchars($uname);
   $pword = htmlspecialchars($pword);

Why?

 

   $db_handle = mysql_connect("$hostname", "$username", "$password");
   $db_found = mysql_select_db("$database", $db_handle);

You don't have to wrap variables in quote tags like that.

 

            session_start();

That line should be at the very top.

 

            $_SESSION['pword'] = $pword;

Is there a need to store a password?

Link to comment
https://forums.phpfreaks.com/topic/157093-solved-login-help/#findComment-827533
Share on other sites

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.