jmr3460 Posted June 28, 2009 Share Posted June 28, 2009 I am working on an admin page for a site I am trying to change from shtml and perl to php and mysql. I have created a index page that works good. <?php if (isset($_COOKIE[auth])) { header("Location: authuser.php"); exit; } else { session_start(); $_SESSION[count]++; $msg = "<p>You have been here $_SESSION[count] times.</p>"; if ($_SESSION[count] >= "7"){ echo "You have run out of tries. Please try again later."; exit; } } ?> <html> <head> <title>Login to Admin Site</title> </head> <body> <center> <h1>Login to XYZ Admin Site</h1> <form method="POST" action="authuser.php" style="border:solid red 5px;width:250px;"> <p><strong>Username:</strong><br /> <input type="text" name="username" size="25" maxlength="25"></p> <p><strong>Password:</strong><br /> <input type="password" name="password" size="25" maxlength="25"></p> <p><input type="submit" name="submit" value="Login"></p> </form> </center> </body> </html> I only had the simple form without the $_COOKIE check and $_SESSION. I added $_SESIONS because I did not want people trying again and again hundreds of times to get into the admin page. I added the $_COOKIE because I wanted to link to the page through a folder link. I want to go to the admin if my $_COOKIE[auth] is set. This all works, if the cookie is set the page forwards to the authuser page. The problem is in my authuser page. I want to set it up to check for a cookie name and value (that may have been set from previous login). I have two different users, (admin and simple). I set cookie with same name but different values. How do I check for the name and value? This is an outline of my authuser.php code: <?php //this is administrator html body display $admin = "HTML for admin user"; //this is simple html body display $simple = "HTML for simple user"; //check for cookie or required fields if (isset($_COOKIE[admin])) { echo "<html> <head> <title>Admin Area</title> </head> <body> $admin <br /> $simple </body> </html>"; } //check for cookie or required fields else { (isset($_COOKIE[simple])); echo "<html> <head> <title>Admin Area</title> </head> <body> $simple </body> </html>"; } else //setup names of database and table to use ($db_name = "simplic5_users"; //connect to server and select database $connection = @mysql_connect("localhost", "user", "password") or die(mysql_error()); $db = @mysql_select_db($db_name, $connection) or die(mysql_error()); //build and issue ADMIN query $sql = "SELECT * FROM admin WHERE username = \"$_POST[username]\" AND password = password(\"$_POST[password]\")"; $result = @mysql_query($sql) or die (mysql_error()); //get the number of rows in the result set $num = mysql_numrows($result)); //set a cookie and print display if authorized, //or redirect elsewhere if unauthorized if ($num != 0) { $cookie_name = "auth"; $cookie_value = "admin"; $cookie_expire = "0"; $cookie_domain = "mysite.org"; setcookie($cookie_name, $cookie_value, $cookie_expire, "/" , $cookie_domain, 0); echo " <html> <head> <title>Admin Area</title> </head> <body> $admin <br /> $simple </body> </html>"; } else { //build and issue simple query $sql = "SELECT * FROM simple WHERE username = \"$_POST[username]\" AND password = password(\"$_POST[password]\")"; $result = @mysql_query($sql) or die (mysql_error()); //get the number of rows in the result set $num = mysql_numrows($result); //print a message and set a cookie if authorized, //or redirect elsewhere if unauthorized if ($num != 0) { $cookie_name = "auth"; $cookie_value = "simple"; $cookie_expire = "0"; $cookie_domain = "mysite.org"; setcookie($cookie_name, $cookie_value, $cookie_expire, "/" , $cookie_domain, 0); echo " <html> <head> <title>Admin Area</title> </head> <body> $simple </body> </html>"; } } } This code is producing a unexpected else on line 106 which is right before I set my cookie for the admin. This error will probably be repeated in the section where I set my simple cookie. I am sorry for the length of this post. Quote Link to comment https://forums.phpfreaks.com/topic/164015-solved-unexpected-t_else/ Share on other sites More sharing options...
Philip Posted June 28, 2009 Share Posted June 28, 2009 //check for cookie or required fields else { (isset($_COOKIE[simple])); Are you looking to do this instead? elseif(isset($_COOKIe['simple'])) { Quote Link to comment https://forums.phpfreaks.com/topic/164015-solved-unexpected-t_else/#findComment-865234 Share on other sites More sharing options...
jmr3460 Posted June 28, 2009 Author Share Posted June 28, 2009 Maybe it is. Will this get the value of the cookie? I am still getting my unexpected T_else error. Quote Link to comment https://forums.phpfreaks.com/topic/164015-solved-unexpected-t_else/#findComment-865236 Share on other sites More sharing options...
Philip Posted June 28, 2009 Share Posted June 28, 2009 Right now you have: <?php if(condition) { } else { } else { } else { } That isn't telling PHP how to handle multiple scenarios for your if/else statement. PHP (as would any other language) would read that in plain English as: If condition is true, then execute this. Otherwise, we should do this. Otherwise do this, otherwise do this. See what I mean with the italicized part? If you want to use multiple situations (more than just true/false for one condition) you need to use either a switch or a elseif. In your case you'd most likely want to use elseif: <?php if(condition1 == true) { // condition 1 is true } elseif(condition2 == true) { // condition 2 is true } elseif(condition3 == true) { // condition 3 is true } else { //none of the conditions are true } Quote Link to comment https://forums.phpfreaks.com/topic/164015-solved-unexpected-t_else/#findComment-865239 Share on other sites More sharing options...
jmr3460 Posted June 28, 2009 Author Share Posted June 28, 2009 Ok thanks for that little lesson. That got rid of all of the errors but I guess everything was false. I ended up with a white screen. The way I am setting my cookies I am setting the cookie name as auth, The value of one is admin and the value of the other is simple. Is this the way I should be setting them? Quote Link to comment https://forums.phpfreaks.com/topic/164015-solved-unexpected-t_else/#findComment-865245 Share on other sites More sharing options...
Philip Posted June 28, 2009 Share Posted June 28, 2009 Well, one thing I'd put at the top of your script is: error_reporting(E_ALL); That'll show all your errors. Next thing, when you're using arrays, you should always put quotes around the key name - unless it is a defined constant or a variable. So, if (isset($_COOKIE[admin])) Should be if (isset($_COOKIE['admin'])) Finally, try putting this somewhere near the top of the script: print_r($_COOKIE); and you should see your cookies in there somewhere. Quote Link to comment https://forums.phpfreaks.com/topic/164015-solved-unexpected-t_else/#findComment-865249 Share on other sites More sharing options...
jmr3460 Posted June 29, 2009 Author Share Posted June 29, 2009 Thanks for the help. I decided to create the cookies with different names (admin with value auth) and (simple with value of auth). I made a few changes through the script and everything works now. I made the changes in the script as far as the $_COOKIE[name] to $COOKIE['name'] as suggested from before. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/164015-solved-unexpected-t_else/#findComment-865253 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.