Jump to content

[SOLVED] Login/Registration Help using Sessions


ReeceSayer

Recommended Posts

Right i've got a login/registration form... the form allows me to register but when i log in the form allows me onto the next page but then the actual access page redirects me back to the login page. This may be a really stupid question but i wrote this code last year for my project and have tried to edit it for a new project...

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" 
                                           lang="en">
<head>
   <title>Login</title>
   <meta http-equiv="Content-Type"
         content="text/html; charset=utf-8" />
   <link href="centered.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
body {
background-repeat: repeat;
}
-->
</style></head>
<body>
   <!-- wrapper div for positioning -->
   <div id="container">
      <!-- Header Section -->
      <div id="header">
  
   <img
   src="images/Semper Fi.png"
   alt="Semper Fi"
   longdesc="value"  
   title="Semper Fi"
/>

   <object
   type="application/x-shockwave-flash" 
   data="NCIS.swf" width="400" height="300"">
   <param name="movie" value="NCIS.swf" />
   <param name="controller" value="true" />
   <p>Sorry, your browser is not standards compliant; please try
      <a href="NCIS Slideshow.swf"NCIS.swf</a>
   </p>
</object>

<br />
    <img    
   src="images/NCIS Logo.png"
   alt="NCIS Logo"
   longdesc="value"  
   title="NCIS"
   width="339" height="107" />
   
<br />

      </div>
  
      <!-- Content Section -->
      <div id="content">
  <?php
  $date = date_default_timezone_set('Europe');
	if (date("H") < 12) 
	 echo 'Good morning, ';
	 else
	 echo 'Good afternoon, ';
	echo 'the time is: ' . date('H:i') . ' on ' . date('D M j') .'th' . PHP_EOL;
?>
<br /><br /><br />

<form method="post" action="login2.php">
<label for="username">Username: </label><br>
<input type="text" name="username" id="username"><br>
<label for="password">Password: </label><br>
<input type="password" name="password" id="password"><br>
<input type="submit" name="submit" id="submit" value="Submit"> <a href="register.php"> Register </a>
</form>

<br /><br /><br />

      </div>
  
      <!-- Footer Section -->
       <div id="footer">
      <p>
      <a href="http://validator.w3.org/check/referer"><img
          src="http://www.w3.org/Icons/valid-xhtml10"
          alt="Valid XHTML 1.0!" height="31" width="88" /></a>
    </p>
<p> <a href="About Us.html">About Us </a> </p>

      </div>
  
   </div> 
   <!-- end container -->
</body>  

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" 
                                           lang="en">
<head>
   <title>Login 2</title>
   <meta http-equiv="Content-Type"
         content="text/html; charset=utf-8" />
   <link href="centered.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
body {
background-repeat: repeat;
}
-->
</style></head>
<body>
   <!-- wrapper div for positioning -->
   <div id="container">
      <!-- Header Section -->
      <div id="header">
  
   <img
   src="images/Semper Fi.png"
   alt="Semper Fi"
   longdesc="value"  
   title="Semper Fi"
/>

<br />
    <img    
   src="images/NCIS Logo.png"
   alt="NCIS Logo"
   longdesc="value"  
   title="NCIS"
   width="339" height="107" />
   
<br />

      </div>
  
      <!-- Content Section -->
      <div id="content">
  <?php
  $date = date_default_timezone_set('Europe');
	if (date("H") < 12) 
	 echo 'Good morning, ';
	 else
	 echo 'Good afternoon, ';
	echo 'the time is: ' . date('H:i') . ' on ' . date('D M j') .'th' . PHP_EOL;
// login2.php

// Start a session. Session is explained below.

include("connection.php");

// Same checking stuff all over again.
if(isset($_POST['submit'])) {
if(empty($_POST['username']) || empty($_POST['password'])) {
	echo "Sorry, you have to fill in all forms";
	header("Location: login.php");
	exit;
}
// Create the variables again.
$username = $_POST['username'];
$password = $_POST['password'];
// Encrypt the password again with the md5 hash. 
// This way the password is now the same as the password inside the database.
$password = md5($password);

// Store the SQL query inside a variable. 
// ONLY the username you have filled in is retrieved from the database.
$query = "SELECT username,password 
		  FROM	 `users`
		  WHERE	 username='$username'";

$result = mysql_query($query);
if(!$result) { 
	// Gives an error if the username given does not exist.
	// or if something else is wrong.
	echo "The query failed " . mysql_error();
} else {
	// Now create an object from the data you've retrieved.
	$row = mysql_fetch_object($result);
	// You've now created an object containing the data.
	// You can call data by using -> after $row.
	// For example now the password is checked if they're equal.
	if($row->password != $password) {
		echo "I am sorry, but the passwords are not equal.";
		header("Location: login.php");
		exit;
	}
	// By storing data inside the $_SESSION superglobal,
	// you stay logged in until you close your browser.
	$_SESSION['username'] = $username;
	$_SESSION['sid'] = session_id(); 
	// Make it more secure by storing the user's IP address.
	$_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
	// Now give the success message.
	// $_SESSION['username'] should print out your username.
	echo "Success! ". $_SESSION['username']; echo " You are now logged in " ;
	echo "Please Wait While You Are Redirected To The Home Page... <a href=\"access.php\">Home</a>";
}
}
?>
      </div>
  
      <!-- Footer Section -->
       <div id="footer">
      <p>
      <a href="http://validator.w3.org/check/referer"><img
          src="http://www.w3.org/Icons/valid-xhtml10"
          alt="Valid XHTML 1.0!" height="31" width="88" /></a>
    </p>
<p> <a href="About Us.html">About Us </a> </p>

      </div>
  
   </div> 
   <!-- end container -->
</body>  
</html>

 

<?php 
	if(!isset($_SESSION['username']) || !isset($_SESSION['sid']) ||!isset($_SESSION['ip'])) {
	header("Location: login2.php");
	}
	include("connection.php");
	echo "Welcome, " . $_SESSION['username'] . "<br>";
	echo "You can only access this page if you are logged in.";
?>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
   <title>Access Page</title>
   <meta http-equiv="Content-Type"
         content="text/html; charset=utf-8" />
   <link href="centered.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
body {
background-repeat: repeat;
}
-->
</style></head>
<body>
   <!-- wrapper div for positioning -->
   <div id="container">
      <!-- Header Section -->
      <div id="header">
  
   <img
   src="images/Semper Fi.png"
   alt="Semper Fi"
   longdesc="value"  
   title="Semper Fi"
/>

   <object
   type="application/x-shockwave-flash" 
   data="NCIS.swf" width="400" height="300"">
   <param name="movie" value="NCIS.swf" />
   <param name="controller" value="true" />
   <p>Sorry, your browser is not standards compliant; please try
      <a href="NCIS Slideshow.swf"NCIS.swf</a>
   </p>
</object>

<br />
    <img    
   src="images/NCIS Logo.png"
   alt="NCIS Logo"
   longdesc="value"  
   title="NCIS"
   width="339" height="107" />
   
<br />

      </div>
  
      <!-- Content Section -->
      <div id="content">
<?php
  $date = date_default_timezone_set('Europe');
	if (date("H") < 12) 
	 echo 'Good morning, ';
	 else
	 echo 'Good afternoon, ';
	echo 'the time is: ' . date('H:i') . ' on ' . date('D M j') .'th' . PHP_EOL;
?>
<br />
<p> You can only view this page if you are logged in </p>
      </div>
  
      <!-- Footer Section -->
       <div id="footer">
      <p>
      <a href="http://validator.w3.org/check/referer"><img
          src="http://www.w3.org/Icons/valid-xhtml10"
          alt="Valid XHTML 1.0!" height="31" width="88" /></a>
    </p>
<p> <a href="About Us.html">About Us </a> </p>

      </div>
  
   </div> 
   <!-- end container -->
</body>  
</html>

 

Any Help will be much appreciated thanks!

Archived

This topic is now archived and is closed to further replies.

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