我正试图使用xampp在php中创建一个注册页面,但遇到了一个问题。当我运行php文件时,它会给出以下错误:
注意:上C:''examplep''htdocs''register.php中的未定义索引:UserName3号线
注意:未定义的索引:C:''examplep''htdocs''register.php中的UserSurname在线4
注意:未定义的索引:上C:''examplep''htdocs''register.php中的UserMail第5行
注意:未定义的索引:C:''examplep''htdocs''register.php中的UserPassword在线6
注意:上的C:''examplep''htdocs''register.php中的未定义索引:UserTel7号线
警告:require(class.phpmailer.php)[function.request]:未能开放流:C:''examplep''htdocs''register.php中没有这样的文件或目录在线24
致命错误:require()[function.request]:打开失败'class.phpmailer.php'(include_path='.;C:''examplep''php''PEAR')C: 第24行上的''examplep''htdocs''register.php
我的php文件register.php:
<?php
$username=$_POST['UserName'];
$usersurname=$_POST['UserSurname'];
$usermail=$_POST['UserMail'];
$userpass=$_POST['UserPassword'];
$usertelephone=$_POST['UserTel'];
$con = mysql_connect("localhost","root","rockenpeace");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbe", $con);
mysql_query("INSERT INTO user (UserName,UserSurname,UserMail, UserPassword, UserTel)
VALUES('$username','$usersurname','$usermail','$userpass','$usertelephone')");
mysql_close($con);
try
{
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Mailer = "smtp";
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->CharSet="utf-8";
$mail->Username = "user@gmail.com";
$mail->Password = "password";
$mail->From = "user@gmail.com";
$mail->FromName="DBE Yazılım";
$mail->AddAddress($_POST['usermail']);
$mail->Subject = "Registration Information";
$mail->Body = "Hello your password is " . $userpass;
//$mail->AddAttachment($path);
$mail->Send();
echo 'Message has been sent.';
}
catch(Exception $e)
{
echo 'hata'.$e->getMessage();
}
header("location:confirmation.php"); ?>
在我的html页面中:
<input type="text" name="UserMail" id="UserMail" size="30" />
<input type="text" name="UserName" id="UserName" size="30" />
<input type="text" name="UserSurname" id="UserSurname" size="30" />
<input type="text" name="UserTel" id="UserTel" size="30" />
<input type="password" name="UserPassword" id="UserPassword" size="30" />
看起来像
a) 您的$_POST
超全局是空的(或者至少不包含您认为它包含的内容)。在这样的案件中,首先要怀疑的是这一点。运行print_r($_POST)
查看其中的内容。
假设POST/GET变量即将到来是不安全的。请通过isset()
进行检查,并检查它们是否包含您认为应该包含的内容。简而言之,我们谈论的是验证。
b) 听起来你的include的路径是错误的。同样,这很容易检查。运行echo file_exists('path/to/include')
-如果没有看到1
,则路径是错误的
使用isset
验证$_POST数组中是否存在密钥。
至于失败的include,这意味着该文件不在get_include_path
中,因此您可以使用set_include_path
设置它(注意这可能会影响您的其他相对include),也可以使用absolute path
。
最后,在输入变量上使用mysql_real_escape_string
,否则很容易受到SQL injection
的攻击。
样本
if (!isset($_POST['userName'], $_POST['key2'], .....)) {
// handle this error, you don't have the correct inputs
} else {
// Validate inputs
// Escape inputs prior to inserting into database
$username = mysql_real_escape_string($_POST['userName']);
...
...
// Your queries
}
In:
<input type="text" name="fname" id="UserName" size="30" />
将name="fname"
更改为name="UserName"
对其他字段执行相同操作。
$_POST
使用name
属性,而不是ID
。
您必须检查您的$_POST
变量是使用isset()
还是empty()
设置的。
isset()
示例:
if(isset($_POST['UserName'])) $username = $_POST['UserName'];
else die("Username variable is not set.");
empty()
示例:
if(!empty($_POST['UserName'])) $username = $_POST['UserName'];
else die("Username variable is empty.");
您应该考虑的另一件事是,在使用mysql_real_escape_string()
在SQL查询中使用字符串之前,应该先转义字符串
示例:
$username = mysql_real_escape_string($_POST['UserName']);
您可能还希望将strip_tags()
与转义结合使用。
如果
该页面中输入标记的name属性和您试图在此处检索的内容相同。
喜欢
<input type='submit' name='submit' value='register' />
</form>
在register.php 中
if(isset($_POST['submit']))
{
// value in quotes should be same as name attribute in input tag
$username=$_POST["username"]
..
}
--更新
<form method ='post' action='register.php' >
<input type="text" name="UserMail" id="UserMail" size="30" />
<input type="text" name="fname" id="UserName" size="30" />
<input type="text" name="lname" id="UserSurname" size="30" />
<input type="text" name="telephone" id="UserTel" size="30" />
<input type="password" name="userPassword" id="UserPassword" size="30" />
</form
和寄存器页
if(isset($_POST['UserMail']))
{
$mail =mysql_real_escape_strings($_POST['UserMail']);
}
if(isset($_POST['fname']))
{
$mail =mysql_real_escape_strings($_POST['fname']);
}
if(isset($_POST['lname']))
{
$mail =mysql_real_escape_strings($_POST['lname']);
}