Jump to content

if() help


therelelogo

Recommended Posts

hello again....

 

I don't seem to be grasping how to add my own If() statment onto PHP, i assumed it would be the same as .VB, maybe not, or maybe i'm sick of looking at the code and have missed a classic error.

 

Anyways, my code retrieves the users account type, "admin" "standard" (more to follow) and then SHOULD be redirecting them to the relvant php page, i.e. admin-index.php for admins, member-index.php for standard etc but my error says i have to many }else{ statements and for the life of me i cant figure out which one, everytime i remove the one it says about it moves to another  :'(

 

my code is (only relevant part):

if($result) {
	if(mysql_num_rows($result) == 1) {
		//Login Successful
		session_regenerate_id();
		$member = mysql_fetch_assoc($result);
		$_SESSION['SESS_MEMBER_ID'] = $member['member_id'];
		$_SESSION['SESS_FIRST_NAME'] = $member['firstname'];
		$_SESSION['SESS_LAST_NAME'] = $member['lastname'];
		$_SESSION['SESS_LOGON_NAME'] = $member['login'];
		$_SESSION['SESS_TOWN'] = $member['town'];
		$_SESSION['SESS_COUNTY'] = $member['county'];
		$_SESSION['SESS_EMAIL'] = $member['email_add'];
		$_SESSION['SESS_ACC'] = $member['account_type'];
		$_SESSION['SESS_TOWN_NAME'] = $member['town'];


		session_write_close();

		if($_SESSION['SESS_ACC'] = 'admin') {

		header("location: admin-index.php");
		exit();
}else{
		header("location: member-index.php");
		exit();

	}else {
		//Login failed
		header("location: login-failed.php");
		exit();
	}
}else {
	die("Query failed");
}

 

any help or suggestions are welcomed and appreciated. also, the code redirects all users to the "member-index.php" if i take out the following code:

 

if($_SESSION['SESS_ACC'] = 'admin') {

		header("location: admin-index.php");
		exit();
}else{

 

but obviously that doesnt work as i want it.

 

Thank you in advance

Link to comment
Share on other sites

Here is what i have and it works for me.  I hope this helps you.

 

/ Check database to see if username and the password exist there.
		$query = "SELECT staffMemberID, smFirstName ";
		$query .= "FROM staffMember ";
		$query .= "WHERE smEmail = '{$email}' ";
		$query .= "AND smPassword = '{$password}' ";
		$query .= "LIMIT 1";
		$result_set = mysql_query($query);
		confirm_query($result_set);

		if (mysql_num_rows($result_set) == 1) {
				$found_user = mysql_fetch_array($result_set);
				$_SESSION['logged_in'] = "yes";
				$_SESSION['firstName'] = $found_user['smFirstName'];
				$_SESSION['staffMemberID'] = $found_user['staffMemberID'];
				redirect_to("staffArea.php");
		} else {
				$message = "Email / Password combination incorrect.<br />
				Please make sure your caps lock key is off and try again.";
		}

} 

 

Link to comment
Share on other sites

thank you both for your solutions, i went with ken as it didnt take much to fix my code, although i do like the look of yours mboley and wish that i had something more presentable for reading than what i have.

 

thank you both once more, you have been of much help and i appreciate it

 

Thanks  ;D

Link to comment
Share on other sites

Just to ensure you know, instead of just giving you the solution. 

 

You have the following line in your code:

 

if($_SESSION['SESS_ACC'] = 'admin') {

 

even though this is in an if statement, i believe it sets $_SESSION['SESS_ACC'] to admin

 

remember to always use the double = sign so you should change that line to

 

if($_SESSION['SESS_ACC'] == 'admin') {

 

again the single = is to set a value, and the double == is to validate a value.

Link to comment
Share on other sites

thanks radar, i agree - it's always better to get the understanding than just the solution - much appreciated. i think your right, because with the code i had, no matter who i logged in as i was always directed to the admin page so the code must have been setting the session to admin as you suggested. thanks for clearing that up  :D

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.