Holding a users ID and pwd during a session

Thread Solved

Join Date: Sep 2009
Posts: 4
Reputation: BeckyAnne87 is an unknown quantity at this point 
Solved Threads: 0
BeckyAnne87 BeckyAnne87 is offline Offline
Newbie Poster

Holding a users ID and pwd during a session

 
0
  #1
Sep 23rd, 2009
Hi,

I am having a little access/control issue. I can add a user to the database using a signup page, go back to the login page, and then log in using the new user. but if i click on a link to go to anoher page that requires the user to be logged in, it forgets the user, and directs them to log back in.

this is the code i am using to control access, the file is implemented like this on every protected page:

  1. include ("access.php");

[CODE]
<?php
session_start();

$username = isset($_POST['username']) ? $_POST['username'] : $_SESSION['username'];
$password = isset($_POST['password']) ? $_POST['password'] : $_SESSION['username'];

if(!isset($username))
{
?>
<html>
<head>
<title>The Book.com - Not Signed In</title>
</head>
<body>
<div class = "head">
<p>The Book.com</p>
</div>
<div class = "content">
<p>You are not signed in. Pleas sign in</p>
<form method = "POST" action = "<?=$_SERVER[PHP_SELF]?>">
<label>Username:</label>
<input type = "text" name = "username" maxlength = "100" size = "25" />
<label>Password: </label>
<input type = "password" name = "password" maxlength = "16" size = "25" />
<input type = "submit" value = "Log In" name = "submit" />
</form>
</div>
</html>
<?php
exit; }

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

$dbhost = "localhost";
$dbname = "thebook";
$dbuser = "TheBook";
$dbpass = "thebook";

$dbcon = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname, $dbcon);

$sql = "SELECT * FROM user WHERE username = '$username' AND password = '$password'";
$query = mysql_query($sql, $dbcon);

if(mysql_num_rows($query) == 0)
{
unset($_SESSION['username']);
unset($_SESSION['password']);
?>
<html>
<head>
<title>The Book.com - Access Denied</title>
</head>
<body>
<p>Your username or password was incorrect, or you are not a registered user of the site.
To try logging in again click <a href = "<?=$_SERVER[PHP_SELF]?>">here</a>. T become a registered
member of this site click <a href = "signup.php">here</a>.</p>
</body>
</html>
<?php
exit;
}
?>

[CODE]

any help would be muchly appreciated.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 81
Reputation: Graphix is an unknown quantity at this point 
Solved Threads: 18
Graphix's Avatar
Graphix Graphix is offline Offline
Junior Poster in Training

Re: Holding a users ID and pwd during a session

 
0
  #2
Sep 24th, 2009
Let me first tell you: NEVER give a password through a SESSION UNCODED. I also recommend you simply do the following:

- If a user has logged in (correctly) then a variable named $_SESSION['auth'] is set "true" or "yes" and if it is needed in the rest of the pages, you also set a $_SESSION['user'] or a $_SESSION['user_id']. If you still want to give a password through, please use md5(), sha1() or another encrypt function.

And at each page you do the following:

  1. <?php
  2. session_start();
  3. ?>
  4. ... other HTML
  5. <body>
  6. <?php
  7. if ($_SESSION['auth'] == "yes") {
  8. //
  9. // You show the members only page
  10. //
  11. echo "You are now logged in and are able to see this!!!";
  12. } else {
  13. //
  14. // You either show the login page or a link to the login page,
  15. // example:
  16. echo 'You are not logged in, please go to the <a href="login.php">login page</a>.';
  17. }
  18. ?>
  19. </body>
  20. // Other HTML....

You can also put some javascript in it that redirects the user directly to the login page.

~G
Last edited by Graphix; Sep 24th, 2009 at 4:06 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 4
Reputation: BeckyAnne87 is an unknown quantity at this point 
Solved Threads: 0
BeckyAnne87 BeckyAnne87 is offline Offline
Newbie Poster

Re: Holding a users ID and pwd during a session

 
0
  #3
Sep 24th, 2009
Thanks heaps, that helped alot. I was using some code that a friend gave me, but I think it has a fair few holes in it. I'm new to PHP, so I think I might start my site from the beginning and try your way.
Cheers.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC