Jump to content

login based on access level


ublapach

Recommended Posts

how would i do this heres my code that logs users in and sets a cookie and session variables to hold the information i need but i tryed adding an if else statement after it and i get an error

 

if ($num > 0)
			{
				@$_SESSION['username'] = $row['username'];
				@$_SESSION['fname'] = $row['fname'];
				@$_SESSION['lname'] = $row['lname'];
				@$_SESSION['email'] = $row['email'];
				@$_SESSION['accesslvl'] = $row['accesslvl'];
				@$_SESSION['logged_in'] = TRUE;
				$cookiename = 'ceiscorp.com';
				$cookievalue=rand(100000,999999);
				$_SESSION['cookieverify'] = $cookievalue;
				setcookie($cookiename,$cookievalue,time()+3600,"/");
				$today=date('r');
				mysql_query("UPDATE webusers SET logged_in = '$today' WHERE username = '$username' AND password = '$passwd'") or die (mysql_error());
				if  (@$_SESSION['accesslvl'] = 'admin')
				header("Location:../indexadmin.php");
				exit;			
				else if
				(@$_SESSION['accesslvl'] = 'ceis')
				header("Location:../indexceis.php");
				exit;
				else if
				(@$_SESSION['accesslvl'] = 'cust')
				header("Location:../indexcust.php");
				exit;

			else{
			@$_SESSION['problem'] ="Username or Password are Incorrect Please Try again";
			header ('Location: ../indexlog.php');
			exit;

 

please help

Link to comment
https://forums.phpfreaks.com/topic/102823-login-based-on-access-level/
Share on other sites

 

Looks like you need to enclose your if as well as else if blocks in brackets so they would be like this:

 

				if  (@$_SESSION['accesslvl'] = 'admin') 
                                        {
				   header("Location:../indexadmin.php");
				   exit;			
                                        }
				else if (@$_SESSION['accesslvl'] = 'ceis')
                                        {
				   header("Location:../indexceis.php");
				   exit;
                                        }
				else if (@$_SESSION['accesslvl'] = 'cust')
                                        {
				   header("Location:../indexcust.php");
				   exit;
                                        }

 

See if this works  :)

 

Looks like there are still missing brackets. Let's try this one:

 


if ($num > 0)
			{
				@$_SESSION['username'] = $row['username'];
				@$_SESSION['fname'] = $row['fname'];
				@$_SESSION['lname'] = $row['lname'];
				@$_SESSION['email'] = $row['email'];
				@$_SESSION['accesslvl'] = $row['accesslvl'];
				@$_SESSION['logged_in'] = TRUE;
				$cookiename = 'ceiscorp.com';
				$cookievalue=rand(100000,999999);
				$_SESSION['cookieverify'] = $cookievalue;
				setcookie($cookiename,$cookievalue,time()+3600,"/");
				$today=date('r');
				mysql_query("UPDATE webusers SET logged_in = '$today' WHERE username = '$username' AND password = '$passwd'") or die (mysql_error());
				if  (@$_SESSION['accesslvl'] == 'admin')
                                        {
				   header("Location:../indexadmin.php");
				   exit;			
                                        }
				else if (@$_SESSION['accesslvl'] == 'ceis')
                                        {
				   header("Location:../indexceis.php");
				   exit;
                                        }
				else if (@$_SESSION['accesslvl'] == 'cust')
                                        {
				   header("Location:../indexcust.php");
				   exit;
                                        }
			}
			else
                               {
			        @$_SESSION['problem'] ="Username or Password are Incorrect Please Try again";
			        header ('Location: ../indexlog.php');
			        exit;
                               }

 

Also, you should need to replace = in your conditions to == 

 

I hope we get it right this time  ;)

 

 

here is the entire code for you

 

<?php
require_once "../tracking/db_connx.php";
session_start();

if (isset ($_POST['submit']))
{
if (preg_match('/[!@#$%^&*()-+=`~<>,.?}{|]/', $_POST['username']))
{
	echo "Illegal Characters In Username";
}
else
{
	if (preg_match('/[!@#$%^&*()-+=`~<>,.?}{|]/', $_POST['passwd']))
	{
		echo "Illegal Characters In Password";
	}
	else
	{
		$username = $_POST['username'];
		$password = $_POST['passwd'];
		$sql = "SELECT * FROM webusers WHERE username='$username' AND passwd='$passwd'";
		if ($r = mysql_query ($sql)) 
		{
			$row = mysql_fetch_array ($r);
			$num = mysql_num_rows ($r);
			if ($num > 0)
			{
				@$_SESSION['username'] = $row['username'];
				@$_SESSION['fname'] = $row['fname'];
				@$_SESSION['lname'] = $row['lname'];
				@$_SESSION['email'] = $row['email'];
				@$_SESSION['accesslvl'] = $row['accesslvl'];
				@$_SESSION['logged_in'] = TRUE;
				$cookiename = 'ceiscorp.com';
				$cookievalue=rand(100000,999999);
				$_SESSION['cookieverify'] = $cookievalue;
				setcookie($cookiename,$cookievalue,time()+3600,"/");
				$today=date('r');
				mysql_query("UPDATE webusers SET logged_in = '$today' WHERE username = '$username'") or die (mysql_error());
			       if($_SESSION['accesslvl'] = 'admin'){
				   header("Location:../indexadmin.php");
				   exit;}
				   else if($_SESSION['accesslvl'] = 'ceis'){
				   header("Location:../indexceis.php");
				   exit;}
				   else if($_SESSION['accesslvl'] = 'cust'){
				   header("Location:../indexcust.php");
				   exit;
                                        
				}else{
			    @$_SESSION['problem'] ="Username or Password are Incorrect Please Try again";
			    header ('Location: ../indexlog.php');
			    exit;
                  }				
			}
		}
	}
}
?>

 

You still have a missing closing bracket at the end which should close your first if statement.

 

Can your editor track parenthesis and brackets? I have a Mac so I use bbedit text editor which helps in that and also have nice coloring for keywords, statements, strings, etc..

 

 

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.