Jump to content

[SOLVED] Login Function not working


mike12255

Recommended Posts

Im going to include two peices of code the function file code, and the code with the form and the code calling the function.

 

My problem is everyone i call the function it returns the error message but im using the right login information.

 

login.php:

 

 <?php
		if (isset($_POST['Submit'])){
		$error = login();
		if (isset($error)){
		echo '<table width="397" height="113" border="0" align="center" cellpadding="0" cellspacing="0">';
	echo"<tr><td>$error</td></tr>";
	echo "</table>";

		}

		}


		?>
            
<table width="397" height="113" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr>
             
              <td><form name="form1" method="post" action="login.php">
                <label>
           Username:   <input type="text" name="username" title="username:"id="username">
                </label></tr></td>
           
          
           <tr><td>
              <label>
                Password:    <input type="password" name="password" id="password">
              </label>
            </tr></td>
         <tr><td>
               <label>
                <input type="submit" name="Submit" id="Submit" align="right" value="Submit">
                   </form>
              </label> </td>
            </tr>
          </table>

 

Functions.php:

 

<?php
include ("connect.php");
error_reporting(E_ALL);
function login(){
$user = $_POST['username'];
$pass = $_POST['password'];


$sql = "SELECT * FROM tbl_users WHERE username = '$user' AND password = '$pass'";
$result = mysql_query($sql) or die (mysql_error());

$row = mysql_num_rows($result);

if ($row == 1){
$_SESSION['user'] = $user;
}else{
$error = "Error Invalid username or password";
return $error;
}

}


?>

 

anyone know why i keep getting this error message?

Link to comment
Share on other sites

Is the error message

"Error Invalid username or password"

Also,

 

Though, in the login.php file, shouldn't it call the functions.php file?

 

***EDIT***

 

If the error message is what I asked, then you wont need to call the functions.php file, yet

if ($row == 1){

$_SESSION['user'] = $user;

}else{

 

I believe should know what to do, I'm guessing it doesn't recognise what it is that it has to do. So it gives the error maybe?

Link to comment
Share on other sites

Basically, what I was trying to get at, but you said it in more complicated terms.

 

A Valid Login = Page to display to the user

A Invalid Login = Error

 

I think thats what you were getting at anyway lonewolf217.

Link to comment
Share on other sites

 

login.php:

 

 
<?php

// session_start();
/* REMOVE QUOTES IF SESSION_START ISNT USED IN 
CONNECT.PHP TO INITIALISE SESSION */
require_once "functions.php";         if (isset($_POST['Submit'])){
         $error = login();
         if (isset($error)){
         echo '<table width="397" height="113" border="0" align="center" cellpadding="0" cellspacing="0">';
      echo"<tr><td>$error</td></tr>";
      echo "</table>";
         
         }
         
         }
         
         if ( !isSet($_SESSION['user']) ){
         ?>
            
 <table width="397" height="113" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr>
             
              <td><form name="form1" method="post" action="login.php">
                <label>
           Username:   <input type="text" name="username" title="username:"id="username">
                </label></tr></td>
           
          
           <tr><td>
              <label>
                Password:    <input type="password" name="password" id="password">
              </label>
            </tr></td>
         <tr><td>
               <label>
                <input type="submit" name="Submit" id="Submit" align="right" value="Submit">
                   </form>
              </label> </td>
            </tr>
          </table>
<?php } ?>

 

Functions.php:

 

<?php
include ("connect.php");
error_reporting(E_ALL);
function login(){
$user = mysql_real_escape_string($_POST['username']);
$pass = mysql_real_escape_string($_POST['password']);


$sql = "SELECT * FROM tbl_users WHERE username = '$user' AND password = '$pass'";
$result = mysql_query($sql) or die (mysql_error());

$row = mysql_num_rows($result);

if ($row == 1){
$_SESSION['user'] = $user;
// Header("Location: logged_in.php");
}else{
$error = "Error Invalid username or password";
return $error;
}

}


?>

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.