2013-04-05 85 views
0

我創建了一個用PHP登錄的窗體(顯然,MySQL)。這是PHP代碼:在PHP登錄頁面中使用cookie

<?php 
$host = 'localhost'; 
$db_user = 'root'; 
$db_password = ''; 
$db_name = 'learningsql'; 

$username = stripslashes($_POST['username']); 
$password = stripslashes($_POST['password']); 

if (strlen($username) < 1 || strlen($password) < 1){ 
    echo "Username or password incorrect"; 
} else { 
    $username = trim(filter_var($_POST['username'], FILTER_SANITIZE_STRING)); 
    $password = trim(filter_var($_POST['password'], FILTER_SANITIZE_STRING)); 
    $hash = hash("sha512",$username. $password); 
    $connection = mysqli_connect($host, $db_user, $db_password); 
    $db = mysqli_select_db($connection, $db_name); 
    $result = mysqli_query("SELECT * FROM utenti WHERE username = '$username' AND password = '$hash'"); 
    if(mysqli_num_rows($result)==0{ 
     die("Username or password incorrect"); 
    } else{ 
     $_SESSION['loggedin']=1; 
     $user = mysqli_fetch_array($result); 
     header("location:loggato.php"); 

    } 
} 
?> 

與形式的.html頁面包含PHP代碼會話:

<?php 
session_start(); 
if(isset($_SESSION['loggedin'])) 
    header('location:loggato.html'); 
?> 

但是,我有一個問題。我目前使用登錄的會話,但我想使用cookie。怎麼樣? 感謝您的答案。

+0

stripslashes?你需要將你的PHP版本升級到更少的石器時代。 – 2013-04-05 15:58:35

回答

0

PHP會話存儲cookie以啓用會話。使用功能session_start()開始會話(duhh),會話將設置一個名爲PHPSESSID的cookie。使用一些開發工具在瀏覽器中檢查它。