Page 1 of 1

How to remove E_ALL errors

Posted: Sat Oct 27, 2007 8:49 am
by shivam0101
This is the code i wrote. It works fine without E_ALL error reporting. I read in a php tutorial that it is a good practice to turn error_reporting to E_ALL. If it is turned on, i get Undefined variable: for all the variables. How to debug this? I tried to used isset, it's not showing error for process, member_name, member_passord. Now i am getting
Notice: Undefined variable: member_name in C:\wamp\www\message\index.php on line 36
Notice: Undefined variable: member_password in C:\wamp\www\message\index.php on line 40

should i have to use isset() for all variables? Is there any other method to solve this?


Code: Select all

<?php
   error_reporting(E_ALL);

   require_once('config/includes.php');
   
   if(isset($_POST['process']))                    $process=$_POST['process'];
   if(isset($_POST['member_name']))           $member_name=$_POST['member_name'];
   if(isset($_POST['member_password']))       $member_password=$_POST['member_password'];
   
   if(isset($process))
   {      
      switch($process)
      {
         case 'submit':
         submit();
          
         case 'show_form':
         show_form(); 
   
         default: show_form();
      }
    }
    else
    {
       show_form();
    }
     
   
   function show_form()
   {
     $login_table=
     "<form method='post' action='index.php?process=submit'>
      <table border='1' align='center'>
        <tr>
         <td>Username</td>
         <td><input type='text' name='member_name' value='$member_name'></td>
        </tr>
        <tr>
         <td>Password</td>
         <td><input type='text' name='member_password' value='$member_password'></td>
        </tr>
        <tr>
         <td colspan='2' align='center'><input type='submit' name='submit' value='submit'></td>
        </tr>
     </table>
     </form>";
     
     $data=array('{content}' => $login_table);
     ReadTemplate('templates/main_temp.html', $data);
  }

Posted: Sat Oct 27, 2007 9:00 am
by seppo0010
You should do it with the variables that might not exist, for example all $_POST position.
The best way to fix this case is

Code: Select all

$process = isset($_POST['process']) ? $_POST['process'] : '';
$member_name = isset($_POST['member_name']) ? $_POST['member_name'] : '';
$member_password = isset($_POST['member_password']) ? $_POST['member_password'] : '';
So, after this, the 3 variables will be setted, having either the $_POST value or an empty string if they doesn't exist

Posted: Sat Oct 27, 2007 1:01 pm
by RobertGonzalez
Your logic is a little off. What you are doing in your original is saying:
If the POST var 'process' is set, make a new var called $process from its value. The problem is if the post var is not set then you have no $process var. So referencing it later will throw those errors. One approace is the suggested approach above this one. Another is to do something like:

Code: Select all

<?php
$process = '';
if (isset($_POST['process'])) {
  $process = $_POST['process'];
}
?>
Keep in mind that this is only a sample of logic and should be coupled with security checks, validation, sanitization and filtration along the way.

Posted: Sat Oct 27, 2007 1:14 pm
by s.dot
Everah wrote:

Code: Select all

<?php
$process = '';
if (isset($_POST['process'])) {
  $process = $_POST['process'];
}
?>
==
seppo0010 wrote:

Code: Select all

$process = isset($_POST['process']) ? $_POST['process'] : '';
:P

Posted: Sat Oct 27, 2007 1:22 pm
by RobertGonzalez
Yes, they are the same. It's just that one is a little more readable to a new developer while the other is a faster and less line-intensive version. And if you have never encountered a ternary, then the second one would make your head spin for a bit.

;)

Posted: Sat Oct 27, 2007 9:09 pm
by shivam0101
Thanks for replying.

Code: Select all

switch($process)
   {
    case 'submit':
          submit($member_username, $member_password);
          break;
    
   
    case default:
         show_form('');
         break; 
   }
gives,
Parse error: parse error, unexpected T_DEFAULT in C:\wamp\www\message\index.php on line 16

if i try,

Code: Select all

case '':
         show_form('');
         break;
does not show the above error.



changed to,

Code: Select all

$process=isset($_GET['process']) ? $_GET['process']: '';
   $member_username=isset($_POST['member_username']) ? $_POST['member_username'] : '';
   $member_password=isset($_POST['member_password']) ? $_POST['member_password'] : '';
still getting,

Notice: Undefined variable: member_username in C:\wamp\www\message\index.php on line 46

Notice: Undefined variable: member_password in C:\wamp\www\message\index.php on line 50


full code:

Code: Select all

<?php
   error_reporting(E_ALL);
   require_once('config/includes.php');
   
   $process=isset($_GET['process']) ? $_GET['process']: '';
   $member_username=isset($_POST['member_username']) ? $_POST['member_username'] : '';
   $member_password=isset($_POST['member_password']) ? $_POST['member_password'] : ''; 
   
   switch($process)
   {
    case 'submit':
          submit($member_username, $member_password);
          break;
    
   
    case '':
         show_form('');
         break; 
   }
   

  function submit($member_username, $member_password)
  {
    $query_check=mysql_query("SELECT * FROM members WHERE member_username=". "'$member_username'" . " AND member_password=". "'$member_password'");
    if(mysql_num_rows($query_check) > 0)
    {
      header("Location: ".SITE_URL."/members");
    }
    else
    {
      $message="Invalid login";
      show_form($message);
    }
     
  }
    
     
   
   function show_form($message)
   {
     $login_table=
     "<form method='post' action='index.php?process=submit'>
      <table border='1' align='center'>
        <tr>
         <td>Username</td>
         <td><input type='text' name='member_username' value='$member_username'></td>
        </tr>
        <tr>
         <td>Password</td>
         <td><input type='text' name='member_password' value='$member_password'></td>
        </tr>
        <tr>
         <td colspan='2' align='center'><input type='submit' name='submit' value='submit'></td>
        </tr>
     </table>
     </form>";
     
     $data=array('{heading}'=>'Login', '{content}' => $login_table, '{message}' => $message, '{links}' => '&nbsp;');
     ReadTemplate('templates/main_temp.html', $data);
  }
  

?>

Posted: Sat Oct 27, 2007 9:16 pm
by John Cartwright
because it's just default

Code: Select all

switch ($foo) {
   case 'foobar';
      echo 'foobar';
   break;

   default :
      echo 'default';
}

Posted: Sat Oct 27, 2007 9:23 pm
by shivam0101
Thanks Jcart,

That error vanished.

How to solve,

Notice: Undefined variable: member_username in C:\wamp\www\message\index.php on line 46

Notice: Undefined variable: member_password in C:\wamp\www\message\index.php on line 50

Posted: Sat Oct 27, 2007 9:26 pm
by John Cartwright
You need to pass $member_username to show_form(), since it is not in the same scope of when you initialized it.
Secondly, you should never re-diplay the users password :wink:

Posted: Sat Oct 27, 2007 9:29 pm
by Christopher
You really need to look at your code and think why $member_username and $member_password are not defined at that point in the code. Hint:

Code: Select all

function show_form($member_username, $member_password, $message)