2015-01-22 2 views
0

안녕 내 웹 사이트에 대한 로그인 스크립트에 문제가 생겼습니다 로그인 정보가 올바른 경우 index.html로 사용자를 다시 작성하는 스크립트가 필요합니다. 당신이 전혀 크게 감사하겠습니다 나를 도울 수 있다면 .. 어떤 도움을 크게 감상 할 수PHP 어떻게 성공적으로 로그인 index.html 사용자를 리디렉션

<?php 
include('config.php'); 
?> 

$ousername = ''; 
    //We check if the form has been sent 
    if(isset($_POST['username'], $_POST['password'])) 
    { 
     //We remove slashes depending on the configuration 
     if(get_magic_quotes_gpc()) 
     { 
      $ousername = stripslashes($_POST['username']); 
      $username = mysql_real_escape_string(stripslashes($_POST['username'])); 
      $password = stripslashes($_POST['password']); 
     } 
     else 
     { 
      $username = mysql_real_escape_string($_POST['username']); 
      $password = $_POST['password']; 
     } 
     //We get the password of the user 
     $req = mysql_query('select password,id from users where username="'.$username.'"'); 
     $dn = mysql_fetch_array($req); 
     //We compare the submited password and the real one, and we check if the user exists 
     if($dn['password']==$password and mysql_num_rows($req)>0) 
     { 
      //If the password is good, we dont show the form 
      $form = false; 
      //We save the user name in the session username and the user Id in the session userid 
      $_SESSION['username'] = $_POST['username']; 
      $_SESSION['userid'] = $dn['id']; 
?> 

<?php 
     } 
     else 
     { 
      //Otherwise, we say the password is incorrect. 
      $form = true; 
      $message = 'The username or password is incorrect.'; 
     } 
    } 
    else 
    { 
     $form = true; 
    } 
    if($form) 
    { 
     //We display a message if necessary 
    if(isset($message)) 
    { 
     echo '<div class="message">'.$message.'</div>'; 
    } 
    //We display the form 
?> 

==> 세부 사항을 확인하기 위해 여기

내 스크립트입니다 ... 감사합니다 ... 감사합니다 당신.

+0

사용해보기 : header ('위치 : http://www.example.com/'); –

+2

더 이상 mysql_ * 확장을 사용하면 안됩니다. mysqli 나'PDO'를 살펴보십시오. – muttley91

+4

** [위험! 당신은 SQL 주입을 방지해야합니다!] (** http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) ** [mysql_ * 'functions] (http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). ** 더 이상 유지 관리되지 않으며 공식적으로 사용되지 않습니다 (https :// /wiki.php.net/rfc/mysql_deprecation). ** 대신 [prepared statements] (http://en.wikipedia.org/wiki/Prepared_statement) **에 대해 알아보고 ** [PDO] (http://us1.php.net/pdo)를 사용하십시오. ** –

답변

1

업데이트 : 저를 수정 @Dagon으로 ..

다시 index.html을로 사용자를 리디렉션하려면 다음을 사용할 수 있습니다

header('Location: http://example.com/index.html'); 
exit; 

성공적으로 로그인 한 후.

+2

위치의 전체 URI입니다. –

+0

나는 이것을 이렇게 사용했고 지금까지 문제가 없다. 왜 조금 설명 할 수 있니? – Goro

+0

답변에 대한 좋은 점은 테스트하는 동안 localhost에서 작동한다는 것입니다. –

0

에 사용자 정보를 설정 한 후 header() 기능을 사용하십시오.

// If the password is good, we don't show the form 
$form = false; 
// We save the user name in the session username and the user Id in the session userid 
$_SESSION['username'] = $_POST['username']; 
$_SESSION['userid'] = $dn['id']; 
header('Location: http://example.com/index.html'); 
+0

[위의 설명을보세요] (http://stackoverflow.com/questions/28098621/php-how-to- redirect-user-to-index-html-sucessfully-login-in # comment-44573199). HTTP Location 헤더를 사용할 때 코드를 정의 된 사양에 맞게 올바르게 정렬하려면 *** *** 절대 URL을 사용해야합니다. – War10ck

0

소스 코드가이 로그인 방법에 가장 적합하지는 않지만 로그인 한 후에도 리디렉션해야합니다.

JavaScript로 할 수 있습니다. jQuery API를 사용하면 사이트의 여러 기능을 향상시키는 데 도움이됩니다. 그래서 당신은 눈치 경우

if($dn['password']==$password and mysql_num_rows($req)>0) 
     { 
      //If the password is good, we dont show the form 
      $form = false; 

      echo "<script>$('#idofelement2reload').load('php.php?login=1');</script>"; 

      $_SESSION['username'] = $_POST['username']; 
      $_SESSION['userid'] = $dn['id']; 

     } 

,

echo "<script>$('#idofelement2reload').load('php.php?login=1');</script>"; 

통해로드 함수를 호출 HTML 문서로 선을 기록이 줄 : 그래서 상황에서

내가 당신에게 해결책이 방법을 권 해드립니다 jQuery 사용자가 로그인 한 매개 변수가있는 파일

jQueries 소스 코드를 머리 속에 넣는 것을 잊지 마십시오.

0
if($dn['password']==$password and mysql_num_rows($req)>0) 
     { 
      //If the password is good, we dont show the form 
      $form = false; 
//We save the user name in the session username and the user Id in the session userid 
      $_SESSION['username'] = $_POST['username']; 
      $_SESSION['userid'] = $dn['id']; 
     header('Location:http://sitename.com/index.php'); 
?> 
관련 문제