2013-04-21 111 views
-3

session_register可以存儲多個值嗎?因爲在我的視圖產品頁面中,我可能需要將session_register中的值輸入到數據庫中。session_register可以存儲多個值嗎?

if(!isset($_SESSION['login_school'])) 
{ 
    header("location: ../enter-icas/"); 
} 

這是登錄頁面。

$_SESSION['login_school'] = $myschoolcode; 
header("location: ../view-products/"); 

這是用戶登錄後的產品頁面。

目前上面的代碼存儲學校代碼,我怎樣才能讓會話存儲我的學校名稱呢?

$myschoolcode = ($_POST['schoolcode']); 
$mypassword = ($_POST['password']); 

$sql = "SELECT school_id FROM schools_info WHERE SchoolCode = '$myschoolcode' and Password 
= '$mypassword'"; 
$result = mysql_query($sql); 
$row = mysql_fetch_array($result); 
$active = $row['active']; 
$count = mysql_num_rows($result); 

// If result matched $myschoolcode and $mypassword, table row must be 1 row 
if($count == 1) { 
session_register("myschoolcode"); 
$_SESSION['login_school'] = $myschoolcode; 

header("location: ../view-products/"); 
} 
+0

使用$ _SESSION ['school_name'] =「學校名稱」; – 2013-04-21 11:14:14

+0

閱讀文檔 - http://www.php.net/manual/en/function.session-register.php - 答案是「是」;但不要讓你的代碼依賴於在下一個版本的PHP中將被棄用的函數 – 2013-04-21 11:17:39

回答

2

您可以分別儲存在兩個會話變量的值,

$_SESSION['login_school_name'] = $login_school_value; 
$_SESSION['login_school_code'] = $myschoolcode; 

如果你想兩個會話變量使用相同的login_school,你可以有數組的選項,

$_SESSION['login_school'] = array(); 
    $_SESSION['login_school']['name'] = $login_school_value; 
    $_SESSION['login_school']['code'] = $myschoolcode; 
+0

非常感謝,感謝你的幫助。 – Rapide 2013-04-21 11:27:40

+0

噢,如果多個會話變量不是一個選項,那比我想到的解決方案更糟糕。 (使用分隔符的單個字符串值,如「學校登錄名稱」)。 – 2013-04-21 11:29:40

+0

@MrLister:請給你的解決方案作爲答案....我會oppurtunity讓你更優雅的解決方案...... – 2013-04-21 11:31:42

相關問題