login script errors.

  • Nem
  • Guru
  • Guru
  • Nem
  • Posts: 1243
  • Loc: UK

Post 3+ Months Ago

I have a form.

I also have a page called "functions.php" with this in it.

PHP Code: [ Select ]
$loginerrors = array (
 
'nouser' => 'Please fill in your username',
 
'nopass' => 'Please fill in your password'  ,
 
'noinput' => 'Please fill in the fields below',
 
'incorrectinput' => 'Your password or username is incorrect'
 
);
 
 
  1. $loginerrors = array (
  2.  
  3. 'nouser' => 'Please fill in your username',
  4.  
  5. 'nopass' => 'Please fill in your password'  ,
  6.  
  7. 'noinput' => 'Please fill in the fields below',
  8.  
  9. 'incorrectinput' => 'Your password or username is incorrect'
  10.  
  11. );
  12.  
  13.  


The form goes like this:



PHP Code: [ Select ]
             <form method=POST action=$PHP_SELF>
 
                <table width=75% border=0 cellpadding=0 cellspacing=0 bgcolor=#D4D0C8>
 
                  <tr>
 
                    <td align=center> <table width=100% border= 0  cellpadding= 0  cellspacing= 0  bordercolor= #000000 >
 
                        <tr bordercolor= #000000  bgcolor= #D4D0C8 >
 
                          <td colspan= 2 > <div align= center >
 
                   
 
                   
 
                   
 
                   
 
                    </div></td>
 
                        </tr>
 
                        <tr bordercolor= #000000  bgcolor= #D4D0C8 >
 
                          <td colspan= 2 >&nbsp;</td>
 
                        </tr>
 
                        <tr bordercolor= #000000  bgcolor= #D4D0C8 >
 
                          <td width= 42% ><font size= 2  face= Verdana, Arial, Helvetica, sans-serif >Username</font></td>
 
                          <td width= 58% ><input name= username  type= text  size= 25 ></td>
 
                        </tr>
 
                        <tr bordercolor= #000000  bgcolor= #D4D0C8 >
 
                          <td><font size= 2  face='Verdana, Arial, Helvetica, sans-serif'>Password</font></td>
 
                          <td> <input name='password' type='text' size='25'></td>
 
                        </tr>
 
                        <tr align='center' valign='middle' bordercolor='#000000' bgcolor='#D4D0C8'>
 
                          <td colspan='2'>&nbsp; </td>
 
                        </tr>
 
                        <tr align='center' valign='middle' bordercolor='#000000' bgcolor='#D4D0C8'>
 
                          <td colspan='2'><input type='submit' name='Submit' value='Login'></td>
 
                        </tr>
 
                      </table></form>
 
 
  1.              <form method=POST action=$PHP_SELF>
  2.  
  3.                 <table width=75% border=0 cellpadding=0 cellspacing=0 bgcolor=#D4D0C8>
  4.  
  5.                   <tr>
  6.  
  7.                     <td align=center> <table width=100% border= 0  cellpadding= 0  cellspacing= 0  bordercolor= #000000 >
  8.  
  9.                         <tr bordercolor= #000000  bgcolor= #D4D0C8 >
  10.  
  11.                           <td colspan= 2 > <div align= center >
  12.  
  13.                    
  14.  
  15.                    
  16.  
  17.                    
  18.  
  19.                    
  20.  
  21.                     </div></td>
  22.  
  23.                         </tr>
  24.  
  25.                         <tr bordercolor= #000000  bgcolor= #D4D0C8 >
  26.  
  27.                           <td colspan= 2 >&nbsp;</td>
  28.  
  29.                         </tr>
  30.  
  31.                         <tr bordercolor= #000000  bgcolor= #D4D0C8 >
  32.  
  33.                           <td width= 42% ><font size= 2  face= Verdana, Arial, Helvetica, sans-serif >Username</font></td>
  34.  
  35.                           <td width= 58% ><input name= username  type= text  size= 25 ></td>
  36.  
  37.                         </tr>
  38.  
  39.                         <tr bordercolor= #000000  bgcolor= #D4D0C8 >
  40.  
  41.                           <td><font size= 2  face='Verdana, Arial, Helvetica, sans-serif'>Password</font></td>
  42.  
  43.                           <td> <input name='password' type='text' size='25'></td>
  44.  
  45.                         </tr>
  46.  
  47.                         <tr align='center' valign='middle' bordercolor='#000000' bgcolor='#D4D0C8'>
  48.  
  49.                           <td colspan='2'>&nbsp; </td>
  50.  
  51.                         </tr>
  52.  
  53.                         <tr align='center' valign='middle' bordercolor='#000000' bgcolor='#D4D0C8'>
  54.  
  55.                           <td colspan='2'><input type='submit' name='Submit' value='Login'></td>
  56.  
  57.                         </tr>
  58.  
  59.                       </table></form>
  60.  
  61.  



Of course the form will submit to itself. So above the form i have:

PHP Code: [ Select ]
if (isset($_POST['submit'])) {
 
 
 
$username = $_POST['username'];
 
$password = $_POST['password'];
 
include  "../req/connect.php";
 
$result = mysql_query("SELECT * FROM gs_admin");
 
 
 
..... rest i got to type up
 
 
  1. if (isset($_POST['submit'])) {
  2.  
  3.  
  4.  
  5. $username = $_POST['username'];
  6.  
  7. $password = $_POST['password'];
  8.  
  9. include  "../req/connect.php";
  10.  
  11. $result = mysql_query("SELECT * FROM gs_admin");
  12.  
  13.  
  14.  
  15. ..... rest i got to type up
  16.  
  17.  


Anyway, In the form i want to have a type of function for example: $ERROR.

So when there is for example no username input, it could be something like:

PHP Code: [ Select ]
if ($_POST['username'] = 0) {
 
$ERROR = echo $loginerrors['nouser'] }
  1. if ($_POST['username'] = 0) {
  2.  
  3. $ERROR = echo $loginerrors['nouser'] }


So in the form, it will show:

Quote:
'Please fill in your username'


As there is no username inputted. I hope you understand what i mean.

Is this possible? Am i doing anything wrong? I am actually confused.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Cafu
  • Student
  • Student
  • Cafu
  • Posts: 97

Post 3+ Months Ago

PHP Code: [ Select ]
if ($_POST['username'] = 0) {
 
$ERROR = echo $loginerrors['nouser'] }
  1. if ($_POST['username'] = 0) {
  2.  
  3. $ERROR = echo $loginerrors['nouser'] }


The concept works, but the code above won't.

First of all, use two equal signs when you are doing comparisons. Secondly, you probably don't want to be comparing it to zero. Checking to see if its equal to "" would work, but testing the length of the string would probably work better.

Code: [ Select ]
if (strlen($_POST['username']) <= 0) { 

The second line looks like you are trying to do two things, set the value of the variable $ERROR and echo the error message. You probably want to do one or the other like this:
Code: [ Select ]
$ERROR = $loginerrors['nouser'];
or:
Code: [ Select ]
echo $loginerrors['nouser'];
  • Nem
  • Guru
  • Guru
  • Nem
  • Posts: 1243
  • Loc: UK

Post 3+ Months Ago

it works :P, fixed it a bit of time ago thanks to Scorpiux.

I got another problem which i will explain in my next post in this thread.
  • Nem
  • Guru
  • Guru
  • Nem
  • Posts: 1243
  • Loc: UK

Post 3+ Months Ago

ok, hows this:

PHP Code: [ Select ]
 
if (isset($_POST['submit')) {
 
 
 
//--------------- making it secure here
 
 
 
    $user = ($_POST,"username");
 
    $pass = md5($_POST,"password");
 
   
 
 
 
//---------- now bring in this file
 
include "../req/functions.php";
 
//errors
 
if ($user == "" || $pass == "") {  $uerror = $loginerrors['noinput']; }
 
elseif ($user == "") { $uerror = $loginerrors['nouser']; }
 
elseif ($pass == "") { $uerror = $loginerrors['nopass'];
 
} else {
 
 
 
//now compare to database
 
//----connect first
 
include "../req/connect.php";
 
    $result = mysql_query("SELECT user,pass FROM gs_admin WHERE user='".$user."' and pass='".$pass."';");
 
    while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
 
        $db_user = $row["user"];
 
        $db_pass = $row["pass"];
 
    }
 
 
 
    if(!$db_user || !$db_pass){ $uerror = $loginerrors['nodata']; }
 
    else{
 
        if($db_user == $user && $db_pass == $pass){
 
         
 
         echo "Welcome Admin - You have Admin Access <a href=admin.php>click here to continue</a>";
 
         
 
        }
 
        else{ echo $uerror = $loginerrors['incorrectinput']; }
 
    }
 
    mysql_free_result($result);
 
   }
 
 
 
 
 
} else {
 
//form stuff here
 
}
 
 
  1.  
  2. if (isset($_POST['submit')) {
  3.  
  4.  
  5.  
  6. //--------------- making it secure here
  7.  
  8.  
  9.  
  10.     $user = ($_POST,"username");
  11.  
  12.     $pass = md5($_POST,"password");
  13.  
  14.    
  15.  
  16.  
  17.  
  18. //---------- now bring in this file
  19.  
  20. include "../req/functions.php";
  21.  
  22. //errors
  23.  
  24. if ($user == "" || $pass == "") {  $uerror = $loginerrors['noinput']; }
  25.  
  26. elseif ($user == "") { $uerror = $loginerrors['nouser']; }
  27.  
  28. elseif ($pass == "") { $uerror = $loginerrors['nopass'];
  29.  
  30. } else {
  31.  
  32.  
  33.  
  34. //now compare to database
  35.  
  36. //----connect first
  37.  
  38. include "../req/connect.php";
  39.  
  40.     $result = mysql_query("SELECT user,pass FROM gs_admin WHERE user='".$user."' and pass='".$pass."';");
  41.  
  42.     while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
  43.  
  44.         $db_user = $row["user"];
  45.  
  46.         $db_pass = $row["pass"];
  47.  
  48.     }
  49.  
  50.  
  51.  
  52.     if(!$db_user || !$db_pass){ $uerror = $loginerrors['nodata']; }
  53.  
  54.     else{
  55.  
  56.         if($db_user == $user && $db_pass == $pass){
  57.  
  58.          
  59.  
  60.          echo "Welcome Admin - You have Admin Access <a href=admin.php>click here to continue</a>";
  61.  
  62.          
  63.  
  64.         }
  65.  
  66.         else{ echo $uerror = $loginerrors['incorrectinput']; }
  67.  
  68.     }
  69.  
  70.     mysql_free_result($result);
  71.  
  72.    }
  73.  
  74.  
  75.  
  76.  
  77.  
  78. } else {
  79.  
  80. //form stuff here
  81.  
  82. }
  83.  
  84.  



for some reason nothing is showing

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 125 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.