Undefined Index Error
Posted: Fri Nov 24, 2006 12:51 am
index.php
function.php
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.
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>Code: Select all
<?php
//工具列
function toolbar(){
if(check_user($_SESSION['id'],$_SESSION['passwd'])){
$main="
<div class='toolbar'>
<a href='{$_SERVER['PHP_SELF']}?op=profile'>帳號設定</a>
<a href='{$_SERVER['PHP_SELF']}?op=logout'>登出</a>
</div>";
}else{
$main="
<div class='toolbar'>
<a href='{$_SERVER['PHP_SELF']}?op=register_form'>註冊</a>
<a href='{$_SERVER['PHP_SELF']}?op=login_form'>登入</a>
</div>";
}
return $main;
}
//註冊表單
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;
}
//註冊
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);
}
//登入表單
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);
}
?>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.