2

我正在尝试登录脚本 burt 我有这个错误 Undefined variable: _session 见页面下方

//checklogin.php

<?php

ob_start();
$host="localhost"; // Host name 
$username="xxxxx"; // Mysql username 
$password="xxxx"; // 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");

// Define $myusername and $mypassword 

$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['name']= myusername;
$_session['pass']= mypassword; 
header("location:login_success.php");
}
 else {
echo "Wrong Username or Password";
}
ob_end_flush();
?>

//login_success.php

// Check if session is not registered, redirect back to main page. 
// Put this code in first line of web page. 
<?php
session_start();
if(!$_session['name']= myusername){
header("location:main_login.php");
}
?>

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

2 回答 2

3

不是$_SESSION$_session还需要session_start()在页面顶部添加

来自 PHP 文档

PHP 中的变量由一个美元符号后跟变量名表示。变量名区分大小写。

于 2012-09-26T15:11:31.870 回答
2

$_SESSION是要大写的。在您的代码中,它都是小写:

if(!$_session['name']= myusername){

更改为(也添加$到我的用户名):

if(!$_SESSION['name']= $myusername){
于 2012-09-26T15:12:10.557 回答