未定义的验证索引

时间:2015-09-05 10:57:56

标签: php validation

我正在尝试使用验证构建一个简单的注册表单。当我将一个字段留空并提交我的表单时,我不断收到此错误未定义的索引电子邮件或未定义的索引密码。例如,我填写除姓氏之外的所有字段我将收到通知,说电子邮件未定义,如果我填写所有字段,我得到用户名,电子邮件和密码未定义。我用谷歌搜索它,我可以得到的消息是isset,我尝试使用isset,但它仍然不起作用。有人可以帮忙吗?

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sign Up</title>
<style>
 label{
  width:100px;
  float:left;
 }
</style>
</head>
<body>
<?php 
 session_start();
 $Firstname=isset($_SESSION['Firstname']);
 $Lastname=isset($_SESSION['Lastname']);
 $username=isset($_SESSION['username']);
 $email=isset($_SESSION['email']);
 $password=isset($_SESSION['password']);
 if(isset($_SESSION['error']))
 {

  echo '<p>'.$_SESSION['error']['Firstname'].'</p>'; 
  echo '<p>'.$_SESSION['error']['Lastname'].'</p>';
  echo '<p>'.$_SESSION['error']['username'].'</p>';
  echo '<p>'.$_SESSION['error']['email'].'</p>';
  echo '<p>'.$_SESSION['error']['password'].'</p>';
   unset($_SESSION['error']);
 }
?>
<div class="signup_form">
<form action="registerUser.php" method="post" >
 <p>
  <label for="Firstname">First Name:</label>
  <input name="Firstname" type="text" id="Firstname" size="30"/>
 </p>
 <p>
  <label for="Lastname">Last Name:</label>
  <input name="Firstname" type="text" id="Lastname" size="30"/>
 </p>
 <p>
  <label for="username">User Name:</label>
  <input name="username" type="text" id="username" size="30"/>
 </p>
 <p>
  <label for="email">E-mail:</label>
  <input name="email" type="text" id="email" size="30"/>
 </p>
 <p>
  <label for="password">Password:</label>
  <input name="password" type="password" id="password" size="30 "/>
 </p>
 <p>
  <input name="submit" type="submit" value="Submit"/>
 </p>
</form>
</div>
<p><a href="login.php">Login</a></p>
</body>
</html>

这是registeruser.php

<?php
session_start();
include('dbconnect.php');
if(isset($_POST['submit']))
{
 //whether the username is blank
 if($_POST['FirstName'] == '')
 {
  $_SESSION['error']['Firstname'] = " FirstName is required.";
 }
 if($_POST['LastName'] == '')
 {
  $_SESSION['error']['Lastname'] = " LastName is required.";
 }
 //whether the email is blank
 if($_POST['email'] == '')
 {
  $_SESSION['error']['email'] = "E-mail is required.";
 }
 else
 {
  //whether the email format is correct
  if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9._-]+)+$/", $_POST['email']))
  {
   //if it has the correct format whether the email has already exist
   $email= $_POST['email'];
  $personcon=$conn;  
  $sql1 = "SELECT * FROM TBLUSERS WHERE email = '$email'";
  $personinfo=oci_parse($personcon,$sql1);
   oci_execute($personinfo);
   oci_free_statement($personinfo);
   if (oci_num_rows($personinfo) > 0)
            {
    $_SESSION['error']['email'] = "This Email is already used.";
   }
  }

  else
  {
   //this error will set if the email format is not correct
   $_SESSION['error']['email'] = "Your email is not valid.";
  }
 }
 //whether the password is blank
 if($_POST['password'] == '')
 {
  $_SESSION['error']['password'] = "Password is required.";
 }
 if($_POST['username'] == '')
 {
  $_SESSION['error']['username'] = "username is required.";
 }

  if(isset($_SESSION['error']))
 {
  header("Location: index.php");
  exit;
 }
 else
 {
  $FirstName = $_POST['FirstName'];
  $LastName = $_POST['LastName'];
  $email = $_POST['email'];
  $username=$_POST['$username'];
  $password = $_POST['password'];


  $sql2 = "INSERT INTO TBLUSERS (FirstName,LastName,email, username,password) VALUES ('$FirstName', $LastName, '$email', '$username','$password')";
  $personinfo2=oci_parse($personcon,$sql2);
   oci_execute($personinfo2);
    oci_free_statement($personinfo2);
  if($personinfo2)
  {
   /* $from=praveen.mohan@students.mq.edu.au */
   $to = $email;
   $subject = "Confirmation from TutsforWeb to $username";
   $header = "TutsforWeb: Confirmation from TutsforWeb";
   $message = "Please click the link below to verify and activate your account. rn";

   $sentmail = mail($to,$subject,$message,$header);

   if($sentmail)
            {
   echo "Your Confirmation link Has Been Sent To Your Email Address.";
   }
   else
         {
    echo "Cannot send Confirmation link to your e-mail address";
   }

  oci_close($personcon);
 }
 }
}
?>

2 个答案:

答案 0 :(得分:2)

如果不填充字段,其索引将不存在于$_POST关联数组中。您需要查看isset是否存在如下:

<?php
session_start();
include('dbconnect.php');
$_SESSION['error'] = array();
if(isset($_POST['submit'])) {
    //whether the username is blank
    if((!isset($_POST['FirstName'])) || ($_POST['FirstName'] == '')) {
        $_SESSION['error']['Firstname'] = " FirstName is required.";
    if((!isset($_POST['LastName'])) || ($_POST['LastName'] == '')) {
        $_SESSION['error']['Lastname'] = " LastName is required.";
    if((!isset($_POST['email'])) || ($_POST['email'] == '')) {
        $_SESSION['error']['email'] = "E-mail is required.";
    } else {
        //whether the email format is correct
        if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9._-]+)+$/", $_POST['email'])) {
            //if it has the correct format whether the email has already exist
            $email= $_POST['email'];
            $personcon=$conn;  
            $sql1 = "SELECT * FROM TBLUSERS WHERE email = '$email'";
            $personinfo=oci_parse($personcon,$sql1);
            oci_execute($personinfo);
            oci_free_statement($personinfo);
            if (oci_num_rows($personinfo) > 0) {
                $_SESSION['error']['email'] = "This Email is already used.";
            }
        } else {
            //this error will set if the email format is not correct
            $_SESSION['error']['email'] = "Your email is not valid.";
        }
    }
    //whether the password is blank
    if((!isset($_POST['password'])) || ($_POST['password'] == '')) {
        $_SESSION['error']['password'] = "Password is required.";
    }
    if((!isset($_POST['username'])) || ($_POST['username'] == '')) {
        $_SESSION['error']['username'] = "username is required.";
    }
    if(isset($_SESSION['error'])) {
        header("Location: index.php");
        exit;
    } else {
        $FirstName = $_POST['FirstName'];
        $LastName = $_POST['LastName'];
        $email = $_POST['email'];
        $username=$_POST['$username'];
        $password = $_POST['password'];

        $sql2 = "INSERT INTO TBLUSERS (FirstName,LastName,email, username,password) VALUES ('$FirstName', $LastName, '$email', '$username','$password')";
        $personinfo2=oci_parse($personcon,$sql2);
        oci_execute($personinfo2);
        oci_free_statement($personinfo2);
        if($personinfo2) {
            /* $from=praveen.mohan@students.mq.edu.au */
            $to = $email;
            $subject = "Confirmation from TutsforWeb to $username";
            $header = "TutsforWeb: Confirmation from TutsforWeb";
            $message = "Please click the link below to verify and activate your account. rn";

            $sentmail = mail($to,$subject,$message,$header);

            if($sentmail) {
                echo "Your Confirmation link Has Been Sent To Your Email Address.";
            } else {
                echo "Cannot send Confirmation link to your e-mail address";
            }

            oci_close($personcon);
        }
    }
}
?>

例如

((!isset($_POST['FirstName'])) || ($_POST['FirstName'] == ''))
如果true中没有'FirstName',或者它是一个空字符串,则

将为$_POST。诀窍是如果第一个操作数是第一个,则不会检查第二个操作数,从而防止问题中提到的问题。

进一步观察:

  • 您的代码假定有一个$_SESSION['error']元素。如果没有正确初始化,您可能会收到错误
  • 您的代码容易受到SQL injection
  • 的攻击
  • 您的代码结构不合理,难以维护
  • 你的代码混合了sql与php,这不是很优雅

答案 1 :(得分:0)

第一个问题是您的HTML输入名称与您期望的PHP名称不匹配。

 if($_POST['FirstName'] == '') //Upper case N

在标记中,您使用<input name = "Firstname" ...小写N

标记的另一个问题是两个输入名为Firstname:

<label for="Lastname">Last Name:</label>
<input name="Firstname" type="text" id="Lastname" size="30"/> <!--Firstname should be Lastname-->

最后还有一个问题出在index.php文件中,在该文件中,您尝试刷新从registerUser.php返回的会话变量。要么必须使用$SESSION["error"]isset(SESSION["error"]["field"]),就像使用registerUser.php中的$POST["field"]一样。

更改后闪烁的代码如下所示:

if(isset($_SESSION['error']))
{
   if (isset($_SESSION['error']['Firstname'])) echo '<p>'.$_SESSION['error']['Firstname'].'</p>'; 
   if (isset($_SESSION['error']['Lastname'])) echo '<p>'.$_SESSION['error']['Lastname'].'</p>';
   if (isset($_SESSION['error']['username'])) echo '<p>'.$_SESSION['error']['username'].'</p>';
   if (isset($_SESSION['error']['email'])) echo '<p>'.$_SESSION['error']['email'].'</p>';
   if (isset($_SESSION['error']['password'])) echo '<p>'.$_SESSION['error']['password'].'</p>';
   unset($_SESSION['error']);
}

我还建议在这个主题上查找一个很好的资源。登录/注册系统很难第一次正确使用。