2011-06-18 122 views
0

我新望到PHP,我試圖用一個登錄系統的位置:PHP登錄系統〜session.register和session.start錯誤

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1"  bgcolor="#CCCCCC"> 
<tr> 
<form action="/?module=admin&n=checklogin" method="post" enctype="multipart/form-data"  name="form1" id="form1"> 
<td> 
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> 
<tr> 
<td colspan="3"><strong>Administrator Login </strong></td> 
</tr> 
<tr> 
<td width="78">Username</td> 
<td width="6">:</td> 
<td width="294"><input type="text" name="myusername" id="myusername"></td> 
</tr> 
<tr> 
<td>Password</td> 
<td>:</td> 
<td><input name="mypassword" type="password" id="mypassword"></td> 
</tr> 
<tr> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td> <button type="submit" name="Submit">Login</button> 
<img src="../images/ajax-loader.gif" width="16" height="16" style="display: none"/> 
<script> 
$("button").click(function() { 
$("img").show("slow"); 
}); 
</script> 
</td> 
</tr> 
</table> 
</td> 
</form> 
</tr> 
</table> 

這是checklogin.php 代碼

<?php 
error_reporting(E_ALL); 
$host="localhost"; // Host name 
$username="david_bpd"; // Mysql username 
$password="documents123456"; // Mysql password 
$db_name="david_bpd"; // Database name 
$tbl_name="members"; // Table name 

// Connect to server and select databse. 
mysql_connect("$host", "$username", "$password")or die(mysql_error()); 
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) or die(mysql_error()); 

// 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"); 
echo "<meta http-equiv='refresh' content='0;url=/?module=admin&n=Login_success'/>"; 
} 
elseif($count==0) { 
echo "<meta http-equiv='refresh' content='0;url=/?module=admin&n=Login_Unsuccessful'/>"; 
} 

?> 

但每當我嘗試用正確的憑據到腰部我得到這些錯誤:

Deprecated: Function session_register() is deprecated in /home/david/public_html/bowlplexdoubles/admin/checklogin.php on line 45 

Warning: session_register() [function.session-register]: Cannot send session cache limiter - headers already sent (output started at /home/david/public_html/bowlplexdoubles/index.php:12) in /home/david/public_html/bowlplexdoubles/admin/checklogin.php on line 45 

Deprecated: Function session_register() is deprecated in /home/david/public_html/bowlplexdoubles/admin/checklogin.php on line 46 

Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively in Unknown on line 0 

我不知道下一步該怎麼做

任何幫助,將不勝感激,

感謝

回答

1

東西被「棄用」的意思,有一個新的/更好的做事方式。在這種情況下,而不是做了session_register你可以創建一個使用

$_SESSION['someKey'] = someValue; 

然後在加載管理頁面的變量,你可以做以下

if (isset($_SESSION['someKey']) { 
    if ($_SESSION['somekey'] == someValue) { 
     //User should be allowed to be on page 
    } 
    else 
     Header("Location: someotherpage.php"); 
} 
else 
    Header("Location: someotherpage.php"); 
+0

感謝您回答問題,我正在使用此代碼來防止人們通過意思他們需要登錄的鏈接進入我們的管理區域: <?php session_start(); (!session_is_registered(myusername)){ session_register(myusername); header(「location:/?module = admin&n = index」); session_unregister(myusername); } ?> 有沒有改變這個呢? –

+0

因此,您可以像這樣設置會話變量。加載管理頁面後,您可以檢查會話變量是否存在,以及是否它是正確的值。如果這些測試中的任何一個失敗,則將用戶重定向到適當的頁面。 – MoarCodePlz

+0

謝謝你能告訴我如何重寫上面列出的checklogin.php,因爲我不確定。謝謝 –

1

您應該使用

<?php session_start();在(以及所有使用會話)的頂部,並設置您的會話變量,如下所示:

$_SESSION['myusername'] = $myusername; 

$_SESSION['mypassword'] = $mypassword;