2012-03-03 95 views
0

目前,如果我登錄然後單擊我的網站上的鏈接標籤,然後返回到登錄頁面,我需要重新輸入詳細信息,但我不希望這是它要存儲會話,直到註銷點擊如何保持會話登錄,直到登出被點擊

PHP登錄腳本:

<? 
session_start(); 

$server = "localhost"; 
$username = "root"; 
$password = ""; 
$db_name = "dblogin"; 

$db = mysql_connect($server,$username,$password) or die("Connection to database failed, perhaps the service is down !!"); 
mysql_select_db($db_name) or die("Database name not available !!"); 

$login = mysql_query("select * from tbuser where (username = '" . $_POST['username'] . "') and (password = '" . md5($_POST['password']) . "')",$db); 
$rowcount = mysql_num_rows($login); 
if ($rowcount == 1) { 
$_SESSION['username'] = $_POST['username']; 
header("Location: securedpage.php"); 
} 
else 
{ 
header("Location: loginpage.php"); 
} 
?> 

回答

2

您需要檢查每一個頁面上的會議,包括loginpage.php

它會像這樣:

<?php 
session_start(); 
if(!isset($_SESSION['username']) 
header("Location: loginpage.php"); 

在您logoutpage.php,你只是session_destroy()

據認爲,這一段代碼將幫助您

+0

所以每一頁都需要是.php然後添加該代碼頭?目前有些是HTML ... – Lukus 2012-03-03 01:22:09

+0

是的@Lukus,如果你想使用PHP會話,所有你將使用會話的文件都需要.php,但是我需要這些文件是.html,你可以嘗試使用HTML5會話存儲:http://demos.w3avenue.com/html5-unleashed-tips-tricks-and-techniques/sample-09-sessionstorage-demo.html – Gerep 2012-03-03 01:26:06

+0

@Lukus這取決於你的服務器和服務器配置,但你也可以告訴網絡服務器將所有'.html'文件視爲'php'。例如:'AddType application/x-httpd-php .html/AddHandler x-httpd-php5 .html//SetHandler application/x- httpd-php/'。 '/'代表新行。 – jeroen 2012-03-03 01:32:56