Jump to content

Home Page Log In Session


mannyguy

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.