Page 1 of 1

Undefined Index Error

Posted: Fri Nov 24, 2006 12:51 am
by hsiwux07
index.php

Code: Select all

<?php

include "setup.php";
include "function.php";
session_start();


$opt=isset($_REQUEST['op']) ? $_REQUEST['op'] : null;
switch ($opt) {
case "register_form":
  $main_content = register_form();
  break;
case "register":
  register($_POST['reg']);
  header("location: {$_SERVER['PHP_SELF']}");
  break;
case "login":
  check_user($_POST["id"],$_POST["passwd"],true);
  header("location: {$_SERVER['PHP_SELF']}");
  break;
case "logout":
  logout();
  header("location: {$_SERVER['PHP_SELF']}");
  break;
case "profile":
  $main_content = register_form($_SESSION["id"]);
  break;
case "modify_profile":
  modify_profile($_POST['reg']);
  header("location: {$_SERVER['PHP_SELF']}");
  break;
default:
  $main_content = (check_user($_SESSION["id"],$_SESSION["passwd"]))?listall():login_form();
  break;
}


?>

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=Big5">
<link rel="stylesheet" type="text/css" media="screen" href="style.css">
<title>Address Book</title>
</head>
<body background="images/bg.gif">

<div class="center_block">
  <img src="images/title.png" class="logo">
  <?php
  echo toolbar();
  echo $main_content;
  ?>
</div>
<div class="copyright">POWERED BY TEST</div>
</body>
</html>
function.php

Code: Select all

<?php
//&#24037;&#20855;&#21015;
function toolbar(){
  if(check_user($_SESSION['id'],$_SESSION['passwd'])){
    $main="
    <div class='toolbar'>
    <a href='{$_SERVER['PHP_SELF']}?op=profile'>&#24115;&#34399;&#35373;&#23450;</a>
    <a href='{$_SERVER['PHP_SELF']}?op=logout'>&#30331;&#20986;</a>
    </div>";
  }else{
    $main="
    <div class='toolbar'>
    <a href='{$_SERVER['PHP_SELF']}?op=register_form'>&#35387;&#20874;</a>
    <a href='{$_SERVER['PHP_SELF']}?op=login_form'>&#30331;&#20837;</a>
    </div>";
  }
  return $main;
}

//&#35387;&#20874;&#34920;&#21934;
function register_form($the_id=""){
  if(!empty($the_id)){
    $op="modify_profile";
    $readonly="readonly";
    $mem=get_mem_data($the_id);

    foreach($mem as $col=>$val){
      $val=stripslashes($val);
      $mem[$col]=$val;
    }
  }else{
    $op="register";
    $readonly="";
  }
  $main=<<<FORM
  <form action="{$_SERVER['PHP_SELF']}" method="post">
  <table class="input_table">
  <tr>
  <td class="col_title">name</td>
  <td class="col"><input type="text" name="reg[name]" value="{$mem['name']}" class="txt"></td>
  </tr>
  <tr>
  <td class="col_title">email</td>
  <td class="col"><input type="text" name="reg[email]" value="{$mem['email']}" class="txt"></td>
  </tr>
  <tr>
  <td class="col_title">email</td>
  <td class="col"><input type="text" name="reg[id]" value="{$mem['id']}" class="txt" $readonly></td>
  </tr>
  <tr>
  <td class="col_title">Password</td>
  <td class="col"><input type="password" name="reg[passwd]" class="txt"></td>
  </tr>
  <tr>
  <td class="col_title">Password</td>
  <td class="col"><input type="password" name="reg[passwd2]" class="txt"></td>
  </tr>
  <td colspan="2" align="center">
  <input type="hidden" name="op" value="{$op}">
  <input type="submit" value="submit" class="input_btn">
  </td>
  </tr>
  </table>
  </form>
FORM;

  return $main;
}

//&#35387;&#20874;
function register($user=array()){
  if(empty($user['id']) or empty($user['passwd']))die("ERROR1");
  if($user['passwd']!=$user['passwd2'])die("ERROR2");
  if(!eregi("[_.0-9a-z-]+@([0-9a-z-]+.)+[a-z]{2,3}$",$user['email']))die("ERROR3");
  if(eregi("[^a-zA-Z0-9]",$user['id']))die("ERROR4");


  $mem=get_mem_data($user['id']);
  if(!empty($mem['id']))die("ERROR5;");

  $fp = fopen (_MEM_FILE, "a+") or die("Can't open "._MEM_FILE." file.");

  while ((list($sn,$name,$email,$id,$passwd) = fgetcsv($fp, 1000))) {
    if(!empty($sn))$i=$sn;
  }
  $new_sn=$i+1;

  if(!get_magic_quotes_gpc()){
    foreach($user as $col=>$val){
      $val=addslashes($val);
      $user[$col]=$val;
    }
  }

  $passwd=md5($user['passwd']);
  $content="{$new_sn},\"{$user['name']}\",\"{$user['email']}\",\"{$user['id']}\",\"{$passwd}\"\n";

  fwrite ($fp, $content,strlen($content));
  fclose($fp);
}

//&#30331;&#20837;&#34920;&#21934;
function login_form(){
  $main=<<<FORM
  <form action="{$_SERVER['PHP_SELF']}" method="post">
  <table class="input_table">
  <tr>
  <td class="col_title">Login</td>
  <td class="col"><input type="text" name="id" class="txt"></td>
  </tr>
  <tr>
  <td class="col_title">Password</td>
  <td class="col"><input type="password" name="passwd" class="txt"></td>
  </tr>
  <td colspan="2" align="center">
  <input type="hidden" name="op" value="login">
  <input type="submit" value="Login;" class="input_btn">
  </td>
  </tr>
  </table>
  </form>
FORM;

  return $main;
}


function get_mem_data($the_id="") {
  if(empty($the_id))return;
  $fp = fopen(_MEM_FILE, "r") or die("Can't open "._MEM_FILE."file.");
  while ((list($sn,$name,$email,$id,$passwd) = fgetcsv($fp, 1000))) {
    if($the_id==$id){
      return array("sn"=>$sn,"name"=>$name,"email"=>$email,"id"=>$id,"passwd"=>$passwd);
    }else{
      continue;
    }
  }
  fclose($fp);
}


function check_user($id="",$passwd="",$md5=false){
  if(empty($id) or empty($passwd))return false ;
  if($md5)$passwd=md5($passwd);
  $user=get_mem_data($id);
  if($user['id']==$id and $user['passwd']==$passwd){
    if(empty($_SESSION["id"])){
      $_SESSION["id"]=$id;
      $_SESSION["passwd"]=$passwd;
      $_SESSION["email"]=$user['email'];
    }
    return true;
  }
  return false;
}


function listall(){
  return "Under Construction";
}


function logout(){
	$_SESSION = array();
	session_destroy();
}


function modify_profile($user=array()){
  if(empty($user['id']) or empty($user['passwd']))die("Improper login");
  if($user['passwd']!=$user['passwd2'])die("Two passwords are not identical.");
  if(!eregi("[_.0-9a-z-]+@([0-9a-z-]+.)+[a-z]{2,3}$",$user['email']))die("Improper Email formate");
  if(eregi("[^a-zA-Z0-9]",$user['id']))die("ERROR");
  if($_SESSION["id"]!=$user['id'])die("ERROR");
  $fp = fopen (_MEM_FILE, "r");
  $content="";
  while ((list($sn,$name,$email,$id,$passwd) = fgetcsv($fp, 1000))) {
    if($id==$user['id']){
      if(!get_magic_quotes_gpc()){
        foreach($user as $col=>$val){
          $val=addslashes($val);
          $user[$col]=$val;
        }
      }
      $passwd=md5($user['passwd']);
      $content.="{$sn},\"{$user['name']}\",\"{$user['email']}\",\"{$id}\",\"{$passwd}\"\n";
      $_SESSION['passwd']=$passwd;
      $_SESSION['email']=$user['email'];
    }else{
      $content.="{$sn},\"{$name}\",\"{$email}\",\"{$id}\",\"{$passwd}\"\n";
    }
  }
  fclose($fp);

  $fp = fopen (_MEM_FILE, "w");
  fwrite ($fp, $content,strlen($content));
  fclose($fp);
}
?>
The output of these codes are below:
Notice: Undefined index: id in c:\program files\easyphp1-8\www\11\s1\index.php on line 33
Notice: Undefined index: passwd in c:\program files\easyphp1-8\www\11\s1\index.php on line 33
Notice: Undefined index: id in c:\program files\easyphp1-8\www\11\s1\function.php on line 4
Notice: Undefined index: passwd in c:\program files\easyphp1-8\www\11\s1\function.php on line 4

Thanks very very much for this.

Posted: Fri Nov 24, 2006 1:04 am
by John Cartwright
you are attempting to use session values which do not exist yet.

Posted: Fri Nov 24, 2006 1:21 am
by dibyendrah
Probably, you need to put global $HTTP_SESSION_VARS; on the function which you are setting session variables.

Code: Select all

function check_user($id="",$passwd="",$md5=false){
  global $_SESSION;
  
}
Dibyendra

Posted: Fri Nov 24, 2006 1:28 am
by John Cartwright
dibyendrah wrote:Propbaly, you need to put global $HTTP_SESSION_VARS; on the function which you are setting session variables.

Code: Select all

function check_user($id="",$passwd="",$md5=false){
  global $_SESSION;
  
}
Dibyendra
$_SESSION is already a super global, p.s. globals are bad and promote code smells.

Posted: Fri Nov 24, 2006 1:44 am
by dibyendrah
Some old version of php maynot support like the new version of php does.

hsiwux07, what is the version of PHP you have in your machine ?

Dibyendra

Posted: Fri Nov 24, 2006 2:02 am
by John Cartwright
dibyendrah wrote:Some old version of php maynot support like the new version of php does.
huh?

Posted: Fri Nov 24, 2006 2:08 am
by hsiwux07
Mine is on PHP 5.

Posted: Fri Nov 24, 2006 3:31 am
by dibyendrah
Jcart wrote:
dibyendrah wrote:Some old version of php maynot support like the new version of php does.
huh?
Sorry Jcart, I might be wrong. But I got the similar problem like this before in PHP 4 and making the global worked. At that moment, I got a similar error like that...
Anyway Jcart, what might be the problem then ? I'm confused. I thought, in function everything will be a local variable and to call the global variable, we have to first make the super global session variable as global first.

With Best Regards,
Dibyendra

Posted: Fri Nov 24, 2006 8:30 am
by feyd
If memory serves, the $_SESSION and more appropriately $HTTP_SESSION_VAR wasn't a super global way way way way back in the early days of PHP 4.

Posted: Fri Nov 24, 2006 11:28 am
by volka
HTTP_SESSION_VARS was and is not superglobal. _SESSION always was (since its introduction in php 4.1)

Posted: Sat Nov 25, 2006 12:43 am
by dibyendrah
Okay.That may be the reason I had to make $HTTP_SESSION_VARS global when I used to use PHP 4. I always thought that $HTTP_SESSION_VARS was made for backward compatibility and $_SESSION is the shortened version of superglobal variable of $HTTP_SESSION_VARS. I mostly use $HTTP_SESSION_VARS for that reason and when using that variable in function, I had to make everytime global. Now I got the idea.

Yes, it's written in manual like following :

Code: Select all

As of PHP 4.1.0, $_SESSION is available as a global variable just like $_POST, $_GET, $_REQUEST and so on. Unlike $HTTP_SESSION_VARS, $_SESSION is always global. Therefore, you do not need to use the global keyword for $_SESSION.
Thank you all this information.