2016-07-26 70 views
-1

我想做一個登錄門戶,導致兩個不同的網頁,連接應該導致根據用戶登錄,它的問題是無論用戶名是什麼進入,只要它是在數據庫中,它會導致寫在代碼的最後位置,我不知道什麼是錯的:一個登錄導致兩個不同的頁面,以兩個不同的用戶

<?php 
// connection to host and database 
$host="localhost"; 
$username="dbusername"; 
$password="dbpass"; 
$db_name="dbname"; 
$tbl_name="members"; 

// connection 
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); 

//username and password sent from 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

//escape strings and stripslashes 
$myusername = stripslashes($myusername); 
$mypassword = stripslashes($mypassword); 
$myusername = mysql_real_escape_string($myusername); 
$mypassword = mysql_real_escape_string($mypassword); 
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; 
$result=mysql_query($sql); 

//to count tables 
$count=mysql_num_rows($result); 

// **no matter the user it logs in, they will land in page2.php** 
if($count==1){ 
    ($myusername == 'user1'){ 
     header('location:page1.php'); 
} 
elseif($myusername == 'user2'){ 
    header('location:page2.php'); 
} 
else { 
    echo "wrong username or password"; 
} 

即去的另一件事是,我不能安全地連接到任何page1.php或page2.php通過session_start();,因爲它不需要登錄進入頁面,它只需要知道鏈接,這就是我在html開始之前在page1.php開頭寫的內容:

<?php 
session_start(); 
if(!$myusername == 'user1'){ 
    header("location:main_login.php"); 
} 
?> 

任何反饋將不勝感激。謝謝。

+0

if($ count == 1)之後有錯誤。 – Dave

+0

首先糾正你寫的條件($ myusername =='user1')..你忘了添加如果.. – sAcH

回答

0

爲什麼我們鼓勵的代碼良好的縮進:

if($count==1) 
{ 
if ($myusername == 'user1') // you forgot an if here 
{ 
    header('location:page1.php'); 
} 
else if ($myusername == 'user2') 
{ 
    header('location:page2.php'); 
} 
else 
{ 
    echo "wrong username or password"; 
} 
}//another closing bracket here 
0

在page1.php中,你如何初始化$myusername變量?

你應該先嚐試一些基本登錄的例子。看來你是跟着this但我相信

a。這個例子很糟糕。

b。您編輯代碼時不理解背後的邏輯。

請閱讀w3school或php manpage的會話示例。

登錄頁面

if (isset($_REQUEST["username"])) { 
    /* 
    usename & password verification here 
    you need to set $_SESSION["username"] in this code 
    */ 

    if ($_SESSION["username"] == "user1") { 
    header("Location: page1.php"); 
    } else if $_SESSION["username"] == "user2") { 
    header("Location: page2.php"); 
    } 
} else { 
    /* login form here */ 
} 

page1.php中

if (!isset($_SESSION["username"])) { 
    header("Location: login.php"); 
} else { 
    /* content of page 1 here */ 
} 

使page2.php

if (!isset($_SESSION["username"])) { 
    header("Location: login.php"); 
} else { 
    /* content of page 2 here */ 
} 
+0

爲了讓頁面在會話正確輸入後被訪問,我使用'if(!_ $ myusername =='user1'){「header(」location:main_login.php「)}'但是如果你有page1.php的鏈接,你可以從任何地方訪問它,而不需要登錄。 –

0

無需算資源ults真的,因爲無論你需要重定向腳本。因此,根據「用戶名」,重定向到特定頁面或錯誤頁面。

switch ($myusername) { 

    case 'user1': 
     $redirectPage = 'page1.php'; 
     break; 

    case 'user2': 
     $redirectPage = 'page2.php'; 
     break; 

    default: 
     $redirectPage = 'error.php'; 
} 

header("Location: $redirectPage"); 
exit; 
+0

好極了,它會重定向到每個用戶名如果你知道用戶,每個頁面都會變得無關緊要,你可以按照用戶必須匹配的密碼來切換嗎? –

+0

然後每個'頁面'必須檢查當前用戶是否可以訪問該特定頁面 - 或重定向。 – MaggsWeb

相關問題