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