Jump to content

Login script not working :(


dennismonsewicz

Recommended Posts

Here is my code:

 

 <?php 

				 if($_POST) {
					//If the form has been submitted

					$host = "localhost";
					$db_username = "username";
					$db_password = "password";
					$db_name = "dbname";

					mysql_connect("$host","$db_username","$db_password")or die("cannot connect");
					mysql_select_db("$db_name")or die("cannt select db");

					$username = stripslashes($_POST['username']);
					$password = stripslashes($_POST['password']);

					$sql = "select * from db_table where username = '$username' and password = '$password'";
					$result = mysql_query($sql);

					$count = mysql_num_rows($result);

					if($count == 1) {
						session_register("username");
						session_register("password");

						session_start();
							  
						header("location: " . SITE_URL . "?username=" . $username . "");

					} else {

						echo '<div class="login">
								<p>Wrong username and password!</p>
								<form action="index.php" method="post" name="loginform">                      
									<input type="text" id="username" name="username" />
									<input type="password" id="password" name="password" style="clear: right;" />
									<input type="image" src="images/loginbutton.jpg" style="width: 66px; height: 33px; float: right; margin-right: 7px;" />
								</form>
								<div class="loginfooter" style="clear: both;"> </div>
							  </div>';

					}
				} else {
					//Else, no post data. Form hasn't been submitted

					if($_SESSION) {
						//Is the user logged in? YES!

						echo '<div class="login">
								<p>Welcome, ' . ucwords($_SESSION['username']) . '</p>
								<p style="text-align: left; padding-left: 10px;"><a href="includes/logout.php">Logout?</a></p>
								<p style="text-align: left; padding-left: 10px;">View Profile</p>
								<p style="text-align: left; padding-left: 10px;">My Photos</p>
								<div class="loginfooter" style="clear: both;"> </div>
							  </div>';
							  
					} else {
						//User isn't logged in
						echo '<div class="login">
								<p>Please Login to Utilize Your Account</p>  
								<form action="index.php" method="post" name="loginform">                      
									<input type="text" id="username" name="username" />
									<input type="password" id="password" name="password" style="clear: right;" />
									<input type="image" src="images/loginbutton.jpg" style="width: 66px; height: 33px; float: right; margin-right: 7px;" />
								</form>
								<div class="loginfooter" style="clear: both;"> </div>
							  </div>';
					}
				}

			?>

 

The username is passed to the URL but does not display the Welcome, $username, the script just keeps reshowing the login form

 

Any ideas?

Link to comment
Share on other sites

<div class="login">
								<p>Please Login to Utilize Your Account</p>  
								<form action="index.php" method="post" name="loginform">                      
									<input type="text" id="username" name="username" />
									<input type="password" id="password" name="password" style="clear: right;" />
									<input type="image" src="images/loginbutton.jpg" style="width: 66px; height: 33px; float: right; margin-right: 7px;" />
								</form>
								<div class="loginfooter" style="clear: both;"> </div>
							  </div>

Link to comment
Share on other sites

This is also wrong

 

if($count == 1) {

session_register("username");

session_register("password");

 

session_start();

 

You want session_start(); at the top and then use

 

$_SESSION['username'] = $_POST['username'];

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.