Jump to content

Need help


dezkit

Recommended Posts

I took a script I found online and played around with it, but if I want to login, after i enter my credentials, i have to refresh the page for the page to actually say that i'm logged in, same thing for logging out.

Thanks

 

<?php
			 	if($_SESSION['uid'] == ""){ 
			 		$login = $_POST["login"];
			 		if($login){
						ob_start();
						include("./regv3_functions.php");
						connect();

						$username = protect($_POST['username']);
						$password = $_POST['password'];


						if(!$username){
						    $errors[] = "You did not supply a username!";
						}

						if(!$username){
						    $errors[] = "You did not supply a password!";
						}


						if(count($errors) >= 1){
						    echo "The following error(s) occured:<br>\n";
						        foreach($errors AS $error){
						            echo $error . "<br>\n";
						        }
						}else {
						   
						    $sql = "SELECT * FROM `users` WHERE `username`='".$username."'";
						    $res = mysql_query($sql) or die(mysql_error());

						    if(mysql_num_rows($res) == 0){
						        echo "The username you supplied does not exist!";
						    }else {
						        $sql2 = "SELECT * FROM `users` WHERE `username`='".$username."' AND `password`='".md5($password)."'";
						        $res2 = mysql_query($sql2) or die(mysql_error());

						        if(mysql_num_rows($res2) == 0){
						            echo "Username and password combination incorrect!";
						        }else {
						            $row = mysql_fetch_assoc($res2);

						            // we're going to set the user id

						            // for sessions
						            $_SESSION['uid'] = $row['id'];


						        }
						    }

						}

						ob_end_flush();
					}
					?> 
				<table width=100%>
				<form method="post" action="">
				<tr><td align=right>Username: <td><input type="text" size=15 name="username"><br>
				<tr><td align=right>Password: <td><input type="password" size=15 name="password"><br>
				<tr><td><td align=left><input type="submit" name="login" value="Login"> <a href="?page=register">Register</a></form>
				</table>
			<?php 
			} else {
				$logout = $_GET["logout"];
				if($logout){
				 session_destroy();
				}
			 echo "You are now logged in<br>";
			 echo "<br>Admin Area<br>";
			 echo "<a href=\"?logout=1\">Log out</a>";
			}
			?>

Link to comment
https://forums.phpfreaks.com/topic/160299-need-help/
Share on other sites

this is because you are checking the value of the session before you run through the rest of your logic.

 

a quick fix is:

 

} else {
$logout = $_GET["logout"];
if($logout){
	 session_destroy();
}
}

if($_SESSION['uid'] != ""){ 
 echo "You are now logged in<br>";
 echo "<br>Admin Area<br>";
 echo "<a href=\"?logout=1\">Log out</a>";
}

 

but i would probably look at re writing this to better suit your needs

Link to comment
https://forums.phpfreaks.com/topic/160299-need-help/#findComment-846661
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.