2014-11-27 2 views
1

내 index.php 파일에이 스 니펫이 있습니다. 사용자에게 로그온하라는 메시지가 표시됩니다. 세부 정보가 데이터베이스 시스템에 없으면 index.php 페이지를 새로 고침하고 "유효하지 않은 자격 증명, 다시 시도하십시오"또는 이와 비슷한 메시지를 표시하려고합니다.PHP, 페이지를 다시로드하고 잘못된 자격 증명 메시지를 표시 하시겠습니까?

어떻게하면됩니까? 마지막 else에서

<?php 
session_start(); 

//Check if user is already logged in 
if(!isset($_POST['id'])) 
{ 
    $message = 'User is logged in already'; 
} 

//Check the username and password have been submitted 
if(!isset($_POST['Username'], $_POST['Password'])) 
{ 
    $message = 'Please enter a valid username and password'; 
} 

//Check the username is the correct length 
else if (strlen($_POST['Username']) > 20 || strlen($_POST['Username']) < 4) 
{ 
    $message = 'Incorrect Username length, please try again'; 
} 

//Check the password is the correct length 
else if (strlen($_POST['Password']) > 20 || strlen($_POST['Password']) < 4) 
{ 
    $message = 'Incorrect Password length, please try again'; 
} 

//Check the username only has alpha numeric characters 
else if (ctype_alnum($_POST['Username']) != true) 
{ 
    $message = "Username must be alpha numeric"; 
} 

else if (ctype_alnum($_POST['Password']) != true) 
{ 
    $message = "Password must be alpha numeric"; 
} 
else 
{ 
    $admin = "****"; 
    $adminPassword = "****"; 

    //Enter the valid data into the database 
    $username = filter_var($_POST['Username'], FILTER_SANITIZE_STRING); 
    $password = filter_var($_POST['Password'], FILTER_SANITIZE_STRING); 

    //Encrypt the password 
    $password = sha1($password); 

    //Connect to the database 
    $SQLusername = ""; 
    $SQLpassword = ""; 
    $SQLhostname = ""; 
    $databaseName = ""; 

    try 
    { 
     //connection to the database 
     $dbhandle = mysql_connect($SQLhostname, $SQLusername, $SQLpassword) 
      or die("Unable to connect to MySQL"); 
     echo "Connected to MySQL<br>"; 

     //select a database to work with 
     $selected = mysql_select_db($databaseName, $dbhandle) 
       or die("Could not select database"); 

     $selectCustomers = "SELECT * FROM 
       customers WHERE name = 
       '$username' AND password = '$password'"; 

     $selectEmployees = "SELECT * FROM 
       employees WHERE name = 
       '$username' AND password = '$password'"; 

     $selectAdmin = "SELECT * FROM 
       employees WHERE name = 
       '$admin' AND password = '$adminPassword'"; 

     $customerResult = mysql_query($selectCustomers) or die(mysql_error()); 
     $employeeResult = mysql_query($selectEmployees) or die(mysql_error()); 
     $adminResult = mysql_query($selectAdmin) or die(mysql_error()); 

     if(mysql_num_rows($customerResult) >= 1) 
     { 
      header("Location: memberPage.php"); 
      $_SESSION["customer"] = "customer"; 
     } 

     else if(mysql_num_rows($employeeResult) >= 1) 
     { 
      header("Location: adminPage.php"); 
      $_SESSION["admin"] = "admin"; 
     } 

     else if(mysql_num_rows($adminResult) >= 1) 
     { 
      header("Location: adminPage.php"); 
      $_SESSION["admin"] = "admin"; 
     } 

     else 
     { 
      header("Location: index.php"); 
     } 

     //close the connection 
     mysql_close($dbhandle); 
    } 
    catch(Exception $e) 
    { 

     $message = 'We are unable to process your request. Please try again later"'; 
    } 


} 
?> 
<html> 
<head> 
<title>Login</title> 
</head> 
<body> 
</body> 
</html> 
+0

당신이 더 많은 코드를지나시겠습니까? 전에 당신이 질문을 한 것 같아요. –

+0

그래서 어떤 오류가 표시되고 .. 어떤 블록이 실행되고 있습니까? "유효하지 않은 자격 증명 ..."을 간단히 표시 할 수 있지만 헤더가 작동하지 않을 수 있습니다. – Riad

+0

물론 지금 전체 index.php 파일을 복사했습니다. – user2757842

답변

2

: 그것을 변경하는

else 
    { 
     header("Location: index.php"); 
     //I would like to return a message here and then reload this page 
    } 

시도 : 여기 내 코드는 "3"가 대기하는 시간 (초)의 수는

else 
    { 
die(header('refresh: 3; url=index.php').'Invalid Credentials, wait 3 seconds or just click <a href="index.php">HERE</a> to check again.'); 
    } 

새로 고침 (페이지 새로 고침).

희망이 도움이 될 수 있습니다!

+0

대단히 감사합니다. 도움을 청할 때 대단히 감사합니다. 답변을 수락하기 전에 5 분 정도 기다려야 만합니다. – user2757842

+0

아무런 문제가 없으면 지금 알려주세요. –

+1

네, 이제 괜찮습니다. 덕분에 – user2757842

1

당신이 함께 시도 할 수 있습니다 :

echo '<script type="text/javascript">alert("invalid credentials");</script>' ; 
echo '<script type="text/javascript">location.reload();</script>' ; 
+0

네, 맞았습니다.하지만 이런 유형의 경우에는 전체 서버 측 구성이 더 좋지만 User2757842는 "** else **" 나는 내 대답에서 언급했다. –

1

당신이 좋아하는 매개 변수를 제공하는 시도 할 수 있습니다 :

header("Location: index.php?err=1");

다음

하여 페이지에 오류가

if($_GET['err']==1) 
 
{ 
 

 
echo "your credentials are wrong"; 
 

 
}
이 도움이 39,

희망 ...