gecko Posted December 13, 2007 Share Posted December 13, 2007 Hi there, I've been having problems with a login script for some time. Basically, its not the Login function that is the problem; it is the type of validation I have used to prevent users who are already logged in from viewing the logged in page. I have put together a code which should print a message to the screen when a user (who is alrready logged in) trys to visit the Login page. The problem is that the message prints to the screen (in a pop-up message) regardless of whether the user is already logged in or not logged in. Of course, I only want to display the message when a logged in user, with a session variable called 'FarmerID' vcisits the page. Its puzzled me, and I haven't known PHP for very long, so I'm hoping that someone else who is more proficient in PHP will be able to help! Here is the code (login.php) <?php session_start(); if ((!isset($_SESSION['attempt_info'])) || ($_SESSION['attempt_info'] == "first_attempt" )) { $_SESSION['attempt_info'] = "first_attempt"; } elseif ($_SESSION['attempt_info'] == "authenticated") { $msg2 = "alert()"; echo "<meta http-equiv='refresh' content='0.1; url=index.html'>"; } else { $_SESSION['attempt_info'] = "unauthenticated"; $msg3 = "You have failed to provide appropriate credentials <br> please try again"; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <SCRIPT LANGUAGE="JavaScript"> <!-- Hide from older browsers alert("You are already logged in to the system!"); // end hiding --> </SCRIPT> <?php if (($_SESSION['attempt_info'] == "unauthenticated") || ($_SESSION['attempt_info'] == "first_attempt")) { ?> <?php } ?> <?php echo $msg3; ?> Thankyou! Quote Link to comment Share on other sites More sharing options...
boushley Posted December 13, 2007 Share Posted December 13, 2007 Well for starters... the popup is being launched at this line <!-- Hide from older browsers alert("You are already logged in to the system!"); // end hiding --> And that is being run no matter what. There is no if statements anywhere around it... For what you're wanting... if you do this <!-- Hide from older browsers <?php if ($_SESSION['attempt_info'] == "authenticated"){ echo 'alert("You are already logged in to the system!");'; } ?> // end hiding --> And then remove the $msg2 = "alert()"; line from your elseif Also... I don't know what this accomplishes... <?php if (($_SESSION['attempt_info'] == "unauthenticated") || ($_SESSION['attempt_info'] == "first_attempt")) { ?> <?php } ?> Because nothing actually happens with that. Quote Link to comment Share on other sites More sharing options...
gecko Posted December 14, 2007 Author Share Posted December 14, 2007 Thanks. The code kinda works- it just doesn't display a message on the screen, thats all. It has stopped printing a message to the screen. Quote Link to comment Share on other sites More sharing options...
nafetski Posted December 14, 2007 Share Posted December 14, 2007 I'm a little bit confused by what you're looking for...you want it so that if a user is already logged in, that he can't go back to the login page? What if they want to log in with another username? =P It' s just strange seeing that kind of function in a login script..usually you're just checking to make sure that the user is logged in to see secured content. However, if that's what you want - I can try to help! =) Some of your code makes no sense (like the fact that it will pop up no matter what, and your if statement that does nothing. Why not try something like... if($_SESSION['attempt_info'] == "authenticated"){ ?> <script language="javascript> alert("You are already logged in to the system!"); </script> <?php } ?> I think this is what you were trying to accomplish at the bottom. Good luck! Quote Link to comment 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.