Jump to content

Login bugged, any reason why? Im not really use to sessions by the way


Jarod

Recommended Posts

Okay I have a site im working on for the login,

 

http://jarodsworld.ismywebsite.com/trookine/index.php

 

You can log in using test for the username and password. When you log out the Member Access Feature is empty, but if you refresh page one more time it shows you are logged in. Any reason why it does that empty box content after redirect (when you successfully log in)? Here is my PHP code:

 

<?php
$login_message = "<p>Login with your existing account to access more features.</p>\n";
if(isset($_POST['islogging'])) { // User is attempting to login
$username = mysql_real_escape_string($_POST['user1']);
$password = mysql_real_escape_string($_POST['pass1']);

$verification_result = $member_handler->verify_member($username, $password);
if($verification_result == 0) { // User details are verified
	$username = $_POST['user1'];
	$_SESSION['username'] = $username;
	$_SESSION['islogged'] = true;
} else {
	$login_message = '<p class="red_text">The submitted username and/or password is incorrect, please try again.</p>'."\n";
}
} else { // Not attempting to login
if(isset($_SESSION['islogged']) == true) { // Check if user is logged in
	if(isset($_GET['option']) == "logout") { // User is logging out --- See if they have clicked the logout button
		header("Location: logout.php");
	}
?>
				<h4 class="hello_member">Hey there, <a href="#" title="Click here to view your profile"><?php print($_SESSION['username']); ?></a>! (<a href="?option=logout">Logout</a>)</h4>
				<p>An announcement was made while you were away. <a href="#">Check it out</a>!</p>
				<h4>Account Statistics</h4>
				<ul class="stats">
					<li><b>Account type:</b><span class="list_info"><?php print($member_handler->member_type($_SESSION['username'])); ?></span></li>
					<li><b>New messages:</b><span class="list_info">0</span></li>
					<li><b>Total messages:</b><span class="list_info">0</span></li>
					<li><b>Total news comments:</b><span class="list_info">0</span></li>
					<li><b>Total guide comments:</b><span class="list_info">0</span></li>
				</ul>
				<h4>Runescape Statistics</h4>
				<ul class="stats">
					<li><b>In-Game Name:</b><span class="list_info"><?php print($member_handler->rs_name($_SESSION['username'])) ?></span></li>
					<li><b>Quests Complete:</b><span class="list_info">0</span></li>
				</ul>		
<?php
} else { // User is not logged in
?>
				<?php print($login_message); ?>
				<p><b>If you don't have an account yet, consider joining TrooKine today!</b></p>
				<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
					<label for="user1">Username:</label>
					<input type="text" name="user1" id="user1" class="field" />
					<label for="pass1">Password:</label>
					<input type="password" name="pass1" id="pass1" class="field" />
					<input type="hidden" name="islogging" />
					<input type="submit" value="Login" />
				</form>
<?php
}
}
?>

A little off topic but this piece of logic doesn't make much sense.

 

$verification_result = $member_handler->verify_member($username, $password);
if($verification_result == 0) { // User details are verified
      $username = $_POST['user1'];
      $_SESSION['username'] = $username;
      $_SESSION['islogged'] = true;
   } else {
      $login_message = '<p class="red_text">The submitted username and/or password is incorrect, please try again.</p>'."\n";
   }

 

0 equates to false in php, surely the verify_member() method should return true if the user is verified. Logic such as this will make your code hard for anyone else to read / follow.

A little off topic but this piece of logic doesn't make much sense.

 

$verification_result = $member_handler->verify_member($username, $password);
if($verification_result == 0) { // User details are verified
      $username = $_POST['user1'];
      $_SESSION['username'] = $username;
      $_SESSION['islogged'] = true;
   } else {
      $login_message = '<p class="red_text">The submitted username and/or password is incorrect, please try again.</p>'."\n";
   }

 

0 equates to false in php, surely the verify_member() method should return true if the user is verified. Logic such as this will make your code hard for anyone else to read / follow.

 

In my functions I always used 0 as true and -1 as false, maybe I should change up on that consider what you just said. I never thought of values being true or false that way to be honest.

A little off topic but this piece of logic doesn't make much sense.

 

$verification_result = $member_handler->verify_member($username, $password);
if($verification_result == 0) { // User details are verified
      $username = $_POST['user1'];
      $_SESSION['username'] = $username;
      $_SESSION['islogged'] = true;
   } else {
      $login_message = '<p class="red_text">The submitted username and/or password is incorrect, please try again.</p>'."\n";
   }

 

0 equates to false in php, surely the verify_member() method should return true if the user is verified. Logic such as this will make your code hard for anyone else to read / follow.

 

In my functions I always used 0 as true and -1 as false, maybe I should change up on that consider what you just said. I never thought of values being true or false that way to be honest.

 

Well, your logic is completely the opposite to that of php. 0 is always considered false and 1 always considered true. You should take a look at booleans.

This would also enable you to perform your checks with less code. eg;

 

if ($member_handler->verify_member($username, $password)) {
  $username = $_POST['user1'];
  $_SESSION['username'] = $username;
  $_SESSION['islogged'] = true;
}

 

instead of....

 

$verification_result = $member_handler->verify_member($username, $password);
if($verification_result == 0) { // User details are verified
  $username = $_POST['user1'];
  $_SESSION['username'] = $username;
  $_SESSION['islogged'] = true;
}

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.