Jump to content

Recommended Posts

  • Replies 64
  • Created
  • Last Reply

Top Posters In This Topic

thats because you are not checking the POSTED info against the database...

 

once $username and $passwords are set you create the session

 

what you need to do is find POSTED username and compare with database info ..

if exists

  create session

else

  go back to relogin

Erm...Ok, bit stuck now. I think it's a bit late for me, school tomorrow XP Here's my function to log people in:

 

<?php
function login($username, $password){
$sql = "SELECT * FROM `members` WHERE username = '$username' AND password = sha1('$password')";
$sqlresult = mysql_query($sql);
if(mysql_num_rows($sqlresult) == 0){

$message = '- Login Failed!';

} elseif(mysql_num_rows($sqlresult) == 1) {

$_SESSION['valid_user'] = $username;
$message = '- Login Successful!';

}else{

$message = '- Login Failed!';

}
}
return $result;
?>

 

Why isn't that enough to sort the problem?

<?

function login($username, $password){
$sql = "SELECT * FROM `members` WHERE username = '$username' AND password = sha1('$password')";
$sqlresult = mysql_query($sql) or die('LOGIN QUERY CHECK ERROR: ' . mysql_error());
$count = mysql_num_rows($sql);

if($count == 0)
   {
       // Do whatver you want to let the user know that LOGIN has failed.

   }
else if($count == 1)
   {
       // Create session and let user know it worked or just redirect them ...
       $_SESSION['valid_user'] = $username;
   }

?>

<?php

function login($username, $password){
$sql = "SELECT * FROM `members` WHERE username = '$username' AND password = sha1('$password')";
$sqlresult = mysql_query($sql) or die('LOGIN QUERY CHECK ERROR: ' . mysql_error());
$count = mysql_num_rows($sql);

if($count == 0)
   {
       $message = '- Login Failed';

   }
else if($count == 1)
   {
       // Create session and let user know it worked or just redirect them ...
       $_SESSION['valid_user'] = $username;
   $message = '- Login Successful!';
   }
}

?>

 

Blank screen after form action.

well yea .. after you create the session you must redirect to a page like index.php or wherever else you want... you can use

 

<meta http-equiv=refresh content='0;url=index.php'>

 

content=0 -- this part sets the time to how long it will take in seconds to redirect to url=page.php

 

so in essence you want

 

else if($count == 1)
   {
       // Create session and let user know it worked or just redirect them ...
       $_SESSION['valid_user'] = $username;
   $message = '- Login Successful!';
     ?> <meta http-equiv=refresh content='0;url=index.php'><?
   }

 

and you have an extra bracket after the else part .. dunno why but it might help to remove it.

<form name="form1" method="post" action="index.php">
    Username: 
    <input name="username" type="text" id="username">
Password: 
<input name="password" type="text" id="password">
<input type="submit" name="Submit" value="Submit"> 
  </form>

Ok so im not sure what the problem is ...

 

step 1 : fill out the form hit submit ... get sent to index.php

 

step 2 : ur on index.php and checking the login...

 

step 3 : once the login passes create $ession and redirect to another page.php

 

... what am i missing ? for now u get the blank page because after

 

$_SESSION['valid_user'] = $username;
$message = '- Login Successful!';

 

the script doesnt know what else to do... unless there's something else happening past that.

OK, problem turned out to be half way down the page, forgot to close a {. Now, every page is showing, you can't log in using any random user and password, but you can't log in with ones that are there, either. Try user: Tom, pass: Test. If it works, the index.php should say, where it says, 'you are not logged in.', next to it should either say 'login failed.' or 'login successful.' But neither happens.

<?php

function login($username, $password){
$sql = "SELECT * FROM `members` WHERE username = '$username' AND password = sha1('$password')";
$sqlresult = mysql_query($sql) or die('LOGIN QUERY CHECK ERROR: ' . mysql_error());
$count = mysql_num_rows($sql);

if($count == 0)
   {
       $message = '- Login Failed';

   }
else if($count == 1)
   {
       // Create session and let user know it worked or just redirect them ...
       $_SESSION['valid_user'] = $username;
   $message = '- Login Successful!';
   }
}

?>

 

Basically, I have a form ($username and $password) that submits to a page. On that page, I am going to include and run this function to validate the user. The only problem is it doesn't work. Due to the fact that the messages don't display, I'm presuming it doesn't work at all.

You dont echo the messages within the function, nor return the $message variable for display outside of the function. To be honest, your best to have functions simply return true or false, then use the calling code to display your message. eg;

 

<?php

function login($username, $password){
 $sql = "SELECT * FROM `members` WHERE username = '$username' AND password = sha1('$password')";
 if ($result = mysql_query($sql)) {
   return mysql_num_rows($sql);
 }
  return false;

}

if (login('foo','bar')) {
 $_SESSION['valid_user'] = 'foo';
 echo "login success";
} else {
 echo "login failed";
}

?>

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.