我正在採取我的第一步到php
,我遇到了這個無法解決我的問題。我正在嘗試創建一個網站,該網站使用簡單的MySQL
數據庫來標識訪問者提供的某些憑據。請注意,在這一點上我對網站的整體安全性並不感興趣。現在的問題:PHP登錄,索引重定向到密碼輸入時檢查憑據
假設我有3個文件。 在文件main_login.php
形式,用戶必須使用進入網站的存儲,它調用checklogin.php
:
<form name="form1" method="post" action="checklogin.php">
在checklogin.php
我檢查由用戶給定的輸入(名和密碼)是否包含在數據庫,如果這是正確的,我重定向他index.php
:
// More unrelated php here....
$sql="SELECT * FROM $tbl_name WHERE username='$username' and password='$password'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $username and $password, table row must be 1 row
if($count==1)
{
// Register $username, $password and redirect to file "index.php"
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
header("location:index.php");
}
else
{
echo "Wrong username or password";
}
終於在index.php
我檢查,如果`$ _SESSION [用戶名]」的定義,否則我將用戶重定向到main_login.php。
<?php
session_start();
if (!isset($_SESSION['username']))
{
header("location: main_login.php");
}
?>
// HTML code here...
的問題現在,也許我已經把太多時間到這一點,我也沒有想清楚,但是每當我輸入正確的用戶名和密碼,我總是重定向到main_login.php,不像我把錯誤的憑據。任何人可能解決這個問題?
你有session_start();在設置$ _SESSION ['username']之前?你爲什麼要存儲密碼?請使用exit();在您設置位置標題之後。不鼓勵使用舊的mysql擴展。 – CodeZombie