Jump to content

[SOLVED] Login Help


Letterbomb05

Recommended Posts

Hey there, After fixing up my Login Script, I *HAD* got it working ok, although I have an error somewhere, and I'm not sure where, I hope someone here can help me fix it. I've had too many errors this week >_>.

 

here are my 2 files, the problem is in 1 of them:

 

MENU.PHP (included into the main page):

<div class="indentmenu">
<ul>
<li><a href="index.php">Home</a></li>
<li><a href="info.php">Info</a></li>
<li><a href="mypage.php">My Page</a></li>
<li><a href="www.team-recoil.com/forums">Forums</a></li>
<li><a href="registerpage.php">Register</a></li>
</ul>
<br style="clear: left" />

<?php
if(isset($_SESSION['registered']) || isset($_SESSION['member']) || isset($_SESSION['moderator']) || isset($_SESSION['admin'])){
?>
<ul>
<li>You are logged in</li>
</ul>
<?php
}else{
?>
<!--login form appearing at top of page if user isn't logged in-->
<form method="post" name="login" action="login.php">
<ul>
<li id="loginform">Username: <input id="username" type="text" name="username" /> Password: <input id="password" type="password" name="password" /></li>
<li> <input type="image" value="Submit" src="login.gif" height="18px" style="padding-top:2px;" /> </li>
</ul>
</form>
<?php
}
?>
</div>

 

 

Login.php:

<?php

//database info
$dbhost = "localhost";
$dbname = "";
$dbuser = "";
$dbpass = "";

//connect to db
mysql_connect($dbhost, $dbuser, $dbpass)or die("Could not connect:".mysql_error());
mysql_select_db($dbname)or die(mysql_error());

//make sure data was entered in the fields
if($_POST['username']!='' && $_POST['password']!='')
{
	//set field data vars
	$username = htmlentities(mysql_real_escape_string($_POST['username']));
	$password = md5($_POST['password']);

	//query db
	$query = "SELECT userid, username, rank FROM users WHERE username='".$username."' AND password='".$password."'";
	$result = mysql_query($query) or die(mysql_error());

	//check to see if there is a db match, if so do the following
	if(mysql_num_rows($result) === 1)
	{
		//set different sessions for different ranks
		$row = mysql_fetch_assoc($result);
		 if($row['rank'] == 1)
		{
			//set the session and redirect user
			$_SESSION['registered'] = TRUE;
			include('info.php');			
		}else{
			//tell the user the account isn't activated if their rank is 0
			echo "Your account is not activated";
		}
	}else{
		//tell the user that they have entered an invalid username or password if this be the case
		echo "Invalid username or password";
	}
}else{
	//if they havn't entered anything in the login form, tell them
	echo "No username/password entered";
}

//end script
?>

 

I also need login.php to set a different session for each rank;

1 = $_SESSION['registered']

2 = $_SESSION['member']

3 = $_SESSION['moderator']

4 = $_SESSION['admin']

 

Anyway, my main problem right now is that when I login, the menu just shows me the Login form, as if no session has been set, it was working earlier, I'm not sure what's gone wrong :S

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

Your problem is either:

 

a.) a session value is not getting assigned, you are forgetting to start_session.

 

b.)

 

		$row = mysql_fetch_assoc($result);
		 if($row['rank'] == 1)
		{
			//set the session and redirect user
			$_SESSION['registered'] = TRUE;
			include('info.php');			
		}else{
			//tell the user the account isn't activated if their rank is 0
			echo "Your account is not activated";
		}

 

This code right here is not being satisfied correct. Eg: Your db value for 'rank' is not set, or not '1'.

 

Thus when,

 

if(isset($_SESSION['registered']) || isset($_SESSION['member']) || isset($_SESSION['moderator']) || isset($_SESSION['admin'])){

 

Is executed, it is going to return false and return the login form.

 

hope that helps.

Link to comment
https://forums.phpfreaks.com/topic/107096-solved-login-help/#findComment-549079
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.