2013-01-02 86 views
0

我在模型和視圖這兩個不同的文件夾中使用以下代碼。在視圖文件夾中包含兩個php文件,如Login.php和Login_success.php。控制器文件夾包含mysql數據庫表字段提取代碼。當我運行下面的代碼時它不能顯示Login_success頁面。只顯示其他字段顯示檢查名稱和密碼。這些所有文件合併到文件夾index.php中。成功登錄後如何顯示歡迎頁面?

這裏我的代碼:

的login.php

<html> 
<head> 
<title>Login</title> 
<link rel ='stylesheet' type = 'text/css' href = 'View/Design.css'> 
<script> 
function Validate(){ 
var x=document.forms["login"]["username"].value; 
if (x==null || x=="") 
    { 
    alert("First name must be filled out"); 
    return false; 
    } 
var x=document.forms["login"]["password"].value; 
if (x==null || x=="") 
    { 
    alert("Password must be filled out"); 
    return false; 
    } 
} 
</script> 
</head> 
<body> 
<form name = 'login' method = 'post' action = 'Controller/Controll.php' > 
<fieldset> 
<legend>Login</legend> 
User Name :<input type = 'text' name = 'username'> 
Password :<input type = 'password' name = 'password'><br><br> 
<input type = 'submit' name = 'submit' value = 'submit' onsubmit = "return Validate()" > 
</fieldset> 
</form> 
    </body> 
</html> 

Controll.php

class Access{ 
function connection(){ 
    require_once('View/Login.php'); 
$con = mysql_connect('localhost','root','root'); 
$db = mysql_select_db('Times_sheet'); 
$query = mysql_query('select * from Login'); 
$row = mysql_fetch_array($query); 
if(isset($_POST['submit'])) 
{ 
    if(($row['UserName']==$_POST['username']) && ($row['Password']==$_POST['password'])){ 
    require_once("View/Login_Success.php"); 
} 
    } 
    else{ 
    echo "Check User name and Password"; 
} 
} 
} 

的index.php

​​

當我輸入正確的用戶名和密碼時,它會穿上空白頁面。我不知道我做了什麼錯誤。

+0

我可以很容易地破解你的系統,只是如果我停用JavaScript在我的瀏覽器中......不要依賴於CLIENT-SIDE驗證! – Yang

回答

0

你只是包括在這一行Login_success.php不是重定向到Login_success.php

if(($row['UserName']==$_POST['username']) && ($row['Password']==$_POST['password'])){ 
    require_once("View/Login_Success.php"); 
} 

所以使用header這種重定向

if(($row['UserName']==$_POST['username']) && ($row['Password']==$_POST['password'])){ 
    header("Location: View/Login_Success.php"); 
    exit(); 
} 
+0

但是,如果(($ row ['UserName'] == $ _ POST ['username'])&&($ row ['Password'] == $ _ POST ['password'])),服務器不檢查條件。它直接進入其他部分。 –

0

在你查詢你取表中的所有(*)的結果'登錄'。

而不是這個查詢表與'WHERE': 例如。 SELECT * FROM登錄其中user = $ _ POST [ '用戶名']和密碼= $ _ POST [ '密碼']

如果結果發現,然後將用戶重定向到您所需的網頁:使用

header("Location : View/Login_Success.php"); 
0

頭php函數用於重定向而不是require_once。像這種格式標題(網址);

0

你必須改變你的代碼,

$query = mysql_query('select * from Login'); 
$row = mysql_fetch_array($query); 
  1. 您可以選擇從MySQL和PHP的檢查後,所有用戶這是不好的方式,你可以把它改成:因爲它是非常(& MD5密碼重要。)
$user = $_POST['username']; 
$pass = MD5($_POST['password']); 
$query = mysql_query("select * from Login WHERE `UserName` = '$user' AND `Password` = '$pass' "); 

如果(mysql_num_rows($查詢)== 1){ // logined }

和保存所有網頁使用會話值的用戶名和用戶:

session_start(); 
$_SESSION['user'] = $row['UserName'] ; 

並用於Login_Success.php您可以使用它this co德:

<?php 

session_start(); 

echo "wellcome user : ".$_SESSION['user'] ; 

?> 

我有2個報價,對您:1。 使用防SQL注入你的代碼。 2.使用標題header('location: View/Login_Success.php');重定向到其他頁面不包括

+0

當我嘗試根據您的指南行終於得到錯誤像「未定義的索引:用戶名,未定義的索引:密碼,mysql_fetch_array()期望參數1是資源,布爾給出」 –

+0

我編輯回答您的代碼信息,並立即修復答案測試。 –

+0

是的,我檢查我什麼時候輸入用戶名和密碼,它顯示空白頁面。如果(mysql_num_rows($ query)== 1){// logined}條件塊無法執行。仍是同樣的問題。 –

0

首先,你的代碼根本就沒有保存;和平黑客

class Access{ 
function connection(){ 
require_once('View/Login.php'); 
$con = mysql_connect('localhost','root','root'); 
$db = mysql_select_db('Times_sheet'); 
$query = mysql_query('select * from Login'); 
$row = mysql_fetch_array($query); 
if(isset($_POST['submit'])) 
{ 

其次,你只包括蛋糕

View/Login_Success.php' 

你要做這樣說:

if(($row['UserName']==$_POST['username']) && ($row['Password']==$_POST['password'])){ 
header('location: View/Login_Success.php'); 
} 
} 
else{ 
    echo "Check User name and Password"; 
} 
} 
} 
+0

是的,我檢查它,但Login_success.php頁面不會打開,因爲代碼if(isset($ _ POST ['submit']))未運行。它直接執行else部分。 –

相關問題