mannyguy Posted December 3, 2011 Share Posted December 3, 2011 Hi. I have problem and I would like to know if anyone here has any ideas on how to fix it. My home page is where my log in form resides. If I leave the home page to browse other pages of my site I want to stay logged in. However, when I return to the home page I see the log in form. The log in form should not be there if I already logged in. Instead I should see the welcome message. Which does appear after loggin in but does not stay on the home page when I leave the page and come back. <?php //Starts the PHP Sesssion session_start(); //Includes the MYSLQ Connection Info from another PHP Page include ('spartacus_mysql.php'); //Submits Log In Info to Database and encrypts Password if (isset ($_POST['loginsubmit'])) { $username = mysql_escape_string($_POST['username']); $userpassword = mysql_escape_string(md5($_POST['userpassword'])); if (!empty ($username) && !empty ($userpassword)) { //Runs Query to Selects the Username and Password from the Database $sql = mysql_query ("SELECT * FROM users WHERE username='".$username."' AND userpassword='".$userpassword."' LIMIT 1"); //Finds and Matches the Log In Info from the Database to the one submitted if(mysql_num_rows ($sql) > 0) { $_SESSION['loggedin'] = true; $_SESSION['username'] = $username; //Runs This Message if the right Username/Password is entered echo '<div id="welcome_member"> Welcome, '.$_SESSION['username'].'.<br /> You are now logged in!<br/> The Combat Tips and Downloads is now accessible!<br /> Enjoy!<br /> </div>'; } else { //Runs This Error if the Username/Password do not match the Database echo '<div id="welcome_member"> Your username and/or password is incorrect! <a href="spartacus_home.php">Try Again</a> </div>'; } } else { //Runs This Code if the Log In Form is Left Blank echo '<div id="welcome_member"> You must enter a username and a password! <a href="spartacus_home.php">Try Again</a> or <a href="spartacus_joinsite.php">Register</a> </div>'; } } else { //This is the code for the Log in Form echo'<div id="join"> <a href="spartacus_joinsite.php">{ JOIN! }</a><br /> </div> <div class="signin"> <div id="titles"> { SIGN IN } </div><br /> <form method="post" action="spartacus_home.php"> <table> <tr><td> Username: <input type="text" name="username" /> </td></tr> <tr><td> Password: <input type="password" name="userpassword"/> </td></tr> <tr><td> <input class="submit" name="loginsubmit" type="submit" /> </tr></td> </table> </form> </div>'; } ?> <!--end sign in PHP--> Here is my code Link to comment https://forums.phpfreaks.com/topic/252405-home-page-log-in-session/ Share on other sites More sharing options...
btellez Posted December 3, 2011 Share Posted December 3, 2011 Your problem is that you check to see if --- isset ($_POST['loginsubmit'])) --- and its always going to be false, if your not arriving at the page right after filling out the form.. you should have your script check to see if, $_SESSION['username'] or $_SESSION['loggedin'] are valid instead in order to display the login form or welcome message. so maybe something like this... if(isset ($_POST['loginsubmit'])) && !isset($_SESSION['loggedin'])){ /*Log in queries etc. here.*/ } else { /* echo login form here*/ } if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) { /* echo welcome message */ } Link to comment https://forums.phpfreaks.com/topic/252405-home-page-log-in-session/#findComment-1294094 Share on other sites More sharing options...
mannyguy Posted December 3, 2011 Author Share Posted December 3, 2011 So do I just add this code in my code? Or is there something I need to remove and replace it with this. Sorry to ask. I'm new at php and still learning Thanks. Link to comment https://forums.phpfreaks.com/topic/252405-home-page-log-in-session/#findComment-1294098 Share on other sites More sharing options...
mannyguy Posted December 3, 2011 Author Share Posted December 3, 2011 It worked thanks! EXCEPT. Now if i go back to the home page the log in form is superimposed on the welcome message. So now What i need is for the log in form to always be gone if I am signed in. <?php //Starts the PHP Sesssion session_start(); //Includes the MYSLQ Connection Info from another PHP Page include ('spartacus_mysql.php'); if(isset ($_POST['loginsubmit']) && !isset($_SESSION['loggedin'])){ /*Log in queries etc. here.*/ $username = mysql_escape_string($_POST['username']); $userpassword = mysql_escape_string(md5($_POST['userpassword'])); if (!empty ($username) && !empty ($userpassword)) { //Runs Query to Selects the Username and Password from the Database $sql = mysql_query ("SELECT * FROM users WHERE username='".$username."' AND userpassword='".$userpassword."' LIMIT 1"); //Finds and Matches the Log In Info from the Database to the one submitted if(mysql_num_rows ($sql) > 0) { $_SESSION['loggedin'] = true; $_SESSION['username'] = $username; } else { //Runs This Error if the Username/Password do not match the Database echo '<div id="welcome_member"> Your username and/or password is incorrect! <a href="spartacus_home.php">Try Again</a> </div>'; } } else { //Runs This Code if the Log In Form is Left Blank echo '<div id="welcome_member"> You must enter a username and a password! <a href="spartacus_home.php">Try Again</a> or <a href="spartacus_joinsite.php">Register</a> </div>';} } else { /* echo login form here*/ //This is the code for the Log in Form echo'<div id="join"> <a href="spartacus_joinsite.php">{ JOIN! }</a><br /> </div> <div class="signin"> <div id="titles"> { SIGN IN } </div><br /> <form method="post" action="spartacus_home.php"> <table> <tr><td> Username: <input type="text" name="username" /> </td></tr> <tr><td> Password: <input type="password" name="userpassword"/> </td></tr> <tr><td> <input class="submit" name="loginsubmit" type="submit" /> </tr></td> </table> </form> </div>'; } if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) { /* echo welcome message */ //Runs This Message if the right Username/Password is entered echo '<div id="welcome_member"> Welcome, '.$_SESSION['username'].'.<br /> You are now logged in!<br/> The Combat Tips and Downloads is now accessible!<br /> Enjoy!<br /> <a href="spartacus_logout.php">Log Out</a> </div>'; } ?> Link to comment https://forums.phpfreaks.com/topic/252405-home-page-log-in-session/#findComment-1294113 Share on other sites More sharing options...
scootstah Posted December 4, 2011 Share Posted December 4, 2011 Try this } else if (!isset($_SESSION['loggedin'])) { /* echo login form here*/ //This is the code for the Log in Form echo'<div id="join"> <a href="spartacus_joinsite.php">{ JOIN! }</a><br /> </div> <div class="signin"> <div id="titles"> { SIGN IN } </div><br /> <form method="post" action="spartacus_home.php"> <table> <tr><td> Username: <input type="text" name="username" /> </td></tr> <tr><td> Password: <input type="password" name="userpassword"/> </td></tr> <tr><td> <input class="submit" name="loginsubmit" type="submit" /> </tr></td> </table> </form> </div>'; } Link to comment https://forums.phpfreaks.com/topic/252405-home-page-log-in-session/#findComment-1294138 Share on other sites More sharing options...
mannyguy Posted December 4, 2011 Author Share Posted December 4, 2011 IT WORKED!!! WORDS CANNOT EXPLAIN HOW MUCH I APPREACIATE THIS! THANK YOU SO MUCH TO BOTH POSTER WHO HELPED ME! I REALLY REALLY REALLY APPRECIATE IT. Link to comment https://forums.phpfreaks.com/topic/252405-home-page-log-in-session/#findComment-1294148 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.