ublapach Posted April 25, 2008 Share Posted April 25, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/102823-login-based-on-access-level/ Share on other sites More sharing options...
moselkady Posted April 25, 2008 Share Posted April 25, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/102823-login-based-on-access-level/#findComment-526702 Share on other sites More sharing options...
ohdang888 Posted April 25, 2008 Share Posted April 25, 2008 whats your error? Quote Link to comment https://forums.phpfreaks.com/topic/102823-login-based-on-access-level/#findComment-526703 Share on other sites More sharing options...
ublapach Posted April 25, 2008 Author Share Posted April 25, 2008 that give a error unexpected { expecting ( Quote Link to comment https://forums.phpfreaks.com/topic/102823-login-based-on-access-level/#findComment-526710 Share on other sites More sharing options...
ohdang888 Posted April 25, 2008 Share Posted April 25, 2008 tghes else{ has to be }else{ and they has to be a closing bracket after the esle statment Quote Link to comment https://forums.phpfreaks.com/topic/102823-login-based-on-access-level/#findComment-526712 Share on other sites More sharing options...
ublapach Posted April 25, 2008 Author Share Posted April 25, 2008 i know that the first thing i tried i think it might be that im trying to do an if if then else Quote Link to comment https://forums.phpfreaks.com/topic/102823-login-based-on-access-level/#findComment-526714 Share on other sites More sharing options...
moselkady Posted April 25, 2008 Share Posted April 25, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/102823-login-based-on-access-level/#findComment-526721 Share on other sites More sharing options...
ublapach Posted April 25, 2008 Author Share Posted April 25, 2008 unxpected t_else error Quote Link to comment https://forums.phpfreaks.com/topic/102823-login-based-on-access-level/#findComment-526729 Share on other sites More sharing options...
moselkady Posted April 25, 2008 Share Posted April 25, 2008 I tested that last code and got no errors. Does it show which line you get that error? Also, could you post the entire code you are using, maybe there are errors somewhere else Quote Link to comment https://forums.phpfreaks.com/topic/102823-login-based-on-access-level/#findComment-526740 Share on other sites More sharing options...
ublapach Posted April 25, 2008 Author Share Posted April 25, 2008 its on line 65 and thats the line that starts with else if (@$_SESSION['accesslvl'] == 'ceis') { Quote Link to comment https://forums.phpfreaks.com/topic/102823-login-based-on-access-level/#findComment-526749 Share on other sites More sharing options...
ohdang888 Posted April 25, 2008 Share Posted April 25, 2008 well i don't know is this matetr or not... but i always use exit(); not exit; and elseif not else if Quote Link to comment https://forums.phpfreaks.com/topic/102823-login-based-on-access-level/#findComment-526776 Share on other sites More sharing options...
conker87 Posted April 25, 2008 Share Posted April 25, 2008 well i don't know is this matetr or not... but i always use exit(); not exit; and elseif not else if Either of those work, I always use 'else if' Quote Link to comment https://forums.phpfreaks.com/topic/102823-login-based-on-access-level/#findComment-526909 Share on other sites More sharing options...
moselkady Posted April 25, 2008 Share Posted April 25, 2008 Could you post your entire code then I can test it for syntax errors? its on line 65 and thats the line that starts with else if (@$_SESSION['accesslvl'] == 'ceis') { Quote Link to comment https://forums.phpfreaks.com/topic/102823-login-based-on-access-level/#findComment-527197 Share on other sites More sharing options...
ublapach Posted April 26, 2008 Author Share Posted April 26, 2008 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; } } } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/102823-login-based-on-access-level/#findComment-527713 Share on other sites More sharing options...
moselkady Posted April 26, 2008 Share Posted April 26, 2008 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.. Quote Link to comment https://forums.phpfreaks.com/topic/102823-login-based-on-access-level/#findComment-527831 Share on other sites More sharing options...
ohdang888 Posted April 27, 2008 Share Posted April 27, 2008 session start() must be the first statement on the php document. Quote Link to comment https://forums.phpfreaks.com/topic/102823-login-based-on-access-level/#findComment-528084 Share on other sites More sharing options...
ublapach Posted April 27, 2008 Author Share Posted April 27, 2008 are you sure cause it worked just like it is until i tried adding the if else statements so it would go to different pages based on user access level Quote Link to comment https://forums.phpfreaks.com/topic/102823-login-based-on-access-level/#findComment-528087 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.