Link to home
Start Free TrialLog in
Avatar of imaw
imaw

asked on

if username and passord blank

why is my error if blank, there is no error message?
when username passworkd is blank it still goes to
the second page(menu)
when it's suppose to show the message on the top of the first page.
<?
   //error_reporting
   error_reporting (E_ALL & ~E_NOTICE);

  //session
#  session_start();
#  $session ->put('username', $username);
#  $session ->put('pwd', $pwd);
#  $session ->put('save', $save);
#  $session ->put('go', $go);

  //cookies
  //  setcookie("accessedBefore", "yes",);

  //declare varibles
  $username= $_GET['username'];
  $pwd= $_GET['pwd'];
  $save = $_GET['save'];
  $go= $_GET['go'];

  // validations (error message)
  if($error == 1)
  {
    print('<p>');
      print('<font color="#0000ff">');
      print("<center>re-submit username again</center>");
      print('</font>');
      print('</p>');
  }      
?>
<html>
<head>
<title>log in </title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name="f" action="menu.php" method="get" >
<table>
      <tr>
          <td bgcolor="#0066CC"> <div align="center"><font color="#FFFFFF"><strong>log
           in to members vault</strong></font> </div>
            </td>
        </tr>
      <tr>
          <td>
             username:  <input type="text" name="username" value="username">
          </td>
     </tr>
      <tr>
          <td>
              password:  <input type="password" name="pwd" value="pwd">
          </td>
     </tr>
     <tr>
        <td>
            save me: <input type="checkbox" name="save" value="save">
         </td>
     </tr>
     <tr>
         <td bgcolor="#0066CC"> <div align="left"><input type="submit" name="submit" value="go"></div>
             </td>
       </tr>
</table>
</form>
</body>
</html>
*************************************************************
page 2 menu.php

************************************************************
<?
  //sessions
# session_start();
 
#  $session->PUT('username', $username);
#  $session->PUT('pwd', $pwd);
#  $session->PUT('save', $save);
#  $session->PUT('go', $go);

  //cookie
 // setcookie("accessedBefore", "yes",);

  //error reporting
  error_reporting (E_ALL & ~E_NOTICE);

 //receiving the varibles from page one.
  $username= $_GET['username'];
  $pwd= $_GET['pwd'];
  $save = $_GET['save'];
  $go= $_GET['go'];
 
  //PHP CODES START
  $validate = true;
 
  if (($username == " ") ||
     ($pwd == ' ') ||
     ($save == ' ') ||
     ($go == ' '))
    {
      //execute if true
        $validate == false;
      $error == 1;
    }
 
#  if(empty($username))
#  {
#   print "You did not enter a user name.";
#  }

 //if login form has been submitted
  if($_GET['go'])
  {
   //send cookie
   setcookie ('yes', $_GET['username']);
   $cookie = TRUE; //cookie has been sent
   print "<p>cookie is LIVE</p>";
  }
   else
   {
     $cookie = FALSE; //cookie has not been sent
     print "<p>sorry, no cookie for you</p>";
   }
   
   if ($validate)
     {
?>
<html>
<head>
<title>menu</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?
   if ($username == "test" || $pwd == "test" )
   {
     print "<font Welcome to the vault";
   }
?>  
    Members vault:
<br>
    <a href="http://www.codygraham.com/hwg/phpweek5/assonecoach.php">COACH</a>
<BR>
        <a href="http://www.codygraham.com/hwg/phpweek5/assoneswing.php">SWING</a>
<BR>
        <a href="http://www.codygraham.com/hwg/phpweek5/assonemind.php">MIND</a>
</body>
</html>
<?
   }
     else
       {
         require ('./login.php');
       }
?>

*************************************************************
Avatar of Zyloch
Zyloch
Flag of United States of America image

What is $errors? You can't use error_reporting for this, you have to do it the old fashioned way, i.e.

$error=array();
if (!isset($_GET['username'])||empty($_GET['username']) {
   $error[]="Empty Username!";
   $validate=false;
}
ASKER CERTIFIED SOLUTION
Avatar of Skonen
Skonen

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Statick001
Statick001

that is the problem, $error == 1 is only comparing, you need a single equal ($error = 1) to achieve what you want

also - your check for a blank username

 if (($username == " ") ||
     ($pwd == ' ') ||
     ($save == ' ') ||
     ($go == ' '))
 
there is a single space in between the quotes. so this will only work if someone enters a single space for the username. to check for an empty string use

if ($username == "")

also you use double quotes for $username and single quotes for the other 3 strings. its best to stick to one method or the other, to keep code neat and readable