教程中的PHP脚本无法按预期工作

时间:2015-06-07 01:34:48

标签: php mysql apache

晚上好!我相信我的php设置可能有问题。我安装了 XAMPP并同时添加了mysql。我从网站http://www.phpeasystep.com/phptu/6.html设置了mysql数据库,main_login.php,checklogin.php(添加了用户和密码信息),login_success.php和checklogin.php,通过剪切和粘贴。我假设代码是正确的。我将它们保存到文件夹C:\ xampp \ htdocs \ UnnamedSite2中。当我尝试运行代码时,输​​入用户名和密码后,我得到以下内容:

注意:未定义的索引:第14行的C:\ xampp \ htdocs \ UnnamedSite2 \ checklogin.php中的myusername

注意:未定义的索引:第15行的C:\ xampp \ htdocs \ UnnamedSite2 \ checklogin.php中的mypassword 用户名或密码错误

可能是什么问题?

main_login.php

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>

checklogin.php

<?php
$host="localhost"; // Host name 
$username="web"; // Mysql username 
$password="foo.bar"; // Mysql password 
$db_name="test"; // Database name 
$tbl_name="members"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword"); 
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

<?php
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>

login_success.php

<html>
<body>
Login Successful
</body>
</html>

Logout.php

<?php 
session_start();
session_destroy();
?>

3 个答案:

答案 0 :(得分:0)

错误表示没有&#39; myusername&#39;和&#39; mypassword&#39;在你的$ _POST数组中。尝试添加:

var_dump($_POST);

在checklogin脚本中的某处检查所述变量的内容。

这将有助于调试

答案 1 :(得分:0)

问题似乎在于Expression Web 4.当我在Dremweaver中运行代码并替换已弃用的全局会话时,一切都开始起作用了。感谢您对此进行调查。

答案 2 :(得分:-1)

我看不出你的错误,但是这个:


error_reporting(E_ALL & ~E_NOTICE);

(使错误消失)