2012-09-26 115 views
2

我想有一個登錄腳本伯特我有這個錯誤 未定義的變量:_session 看到頁面下方PHP登錄腳本未定義的變量:_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> 
+2

你需要capitlize它 - 這是'$ _SESSION' – andrewsi

+0

也'如果(! $ _session ['name'] = myusername)'應該是'if($ _ session ['name']!= myusername)' – nana

回答

3

這是$_SESSION$_session你還需要添加session_start()頁面

的頂部從PHP DOC

Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.

+0

他有session_start()那裏。 – nana

+0

@mistrfu:不在'checklogin.php' –

+0

@mistrfu告訴我你是否可以在'checklogin.php'頂部看到'session_start'? – Baba

2

$_SESSION要大寫的。在你的代碼是全部小寫:

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

變化(還添加$到名爲myUsername):

if(!$_SESSION['name']= $myusername){ 
+0

感謝大家你很好我的錯誤對不起,但現在我有這個使用未定義的常量myusername - 假設'myusername'在/Applications/MAMP/htdocs/test/login_success.php在線5 –

+1

$ myusername,缺少$ – nana

+1

@LefterisLivanos將'$'添加到'myusername'以將其標記爲您之前定義的變量。或者,你可以使它成爲一個常量,並通過在某處使用define()來定義它。 – Sirko