Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

if username and passord blank

Posted on 2004-09-13
3
255 Views
Last Modified: 2006-11-17
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');
       }
?>

*************************************************************
0
Comment
Question by:imaw
3 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 12051194
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;
}
0
 
LVL 4

Accepted Solution

by:
Skonen earned 500 total points
ID: 12051694
In your posted PHP you used:

//execute if true
$validate == false;
$error == 1;

See anything wrong with that? == is a comparison operator, you need to use an assignment operator, such as a single equal sign instead:

//Execute if true
$validate = false;
$error = 1;

0
 
LVL 3

Expert Comment

by:Statick001
ID: 12054982
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
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question