2013-04-16 2 views
0

PHP와 세션에 문제가 있습니다. 이것은 dictionary.php에 대한 나의 코드입니다.PHP 세션 문제

<?php 
if(!isset($_SESSION['auth'])){ 
$_SESSION['auth'] = 0; 
} 
if($_SESSION['auth'] == 0){ 
header("Location: index.php"); 
} 
?> 

그래서, 거기에 0 또는 1로 세션 "인증"으로 설정하도록되어 index.php에 있습니다 :

<?php 
    $msg = ""; 

//Password handler 
if(!isset($_POST['password'])){ 
    $password = ""; 
}else{ 
    $password = $_POST['password']; 
} 

if(!isset($_SESSION['auth'])){ 
    $_SESSION['auth'] = 0; 
} 

//Username handler 
if(!isset($_POST['username'])){ 
    $username = ""; 
}else{ 
     $username = $_POST['username']; 
    if($username == "potato" && $password == "poteto"){ 
     $_SESSION['auth'] = 1; 
     header("Location: dictionary.php"); 
    }else{ 
     $msg = "<br /> 
<font color=\"#FF0000\">Invalid username or password!</font><br />"; 
    } 
} 

if($_SESSION['auth'] == 0){ 
     header("Location: dictionary.php"); 
} 

?> 

(위의 수 있습니다 잘못된 관행이나 뭐,하지만 난 단지 학교 프로젝트를 위해이 일을하고 난 파일 dictionary.php을보고 다른 사람을 원하지 않는다.)

의 index.php는 경우 당신이 천국에 $username$password을 보낼 POST 방법과 양식을 사용합니다 ' 아직 눈치 채지 못했다.

로그인 정보 "감자"와 "poteto"를 입력하면 dictionary.php로 리다이렉트되지만, 즉시 index.php로 다시 보내집니다. 위의 코드를 수정 해 보았지만 행운은 없습니다.

누구든지 나를 보여줘야한다면 URL을 제공 할 수 있습니다.

감사합니다.

+3

어디에도'session_start()'가 보이지 않습니다. 먼저 수정하십시오. 읽기 : http://php.net/manual/en/session.examples.basic.php –

+0

"오류 310 (net :: ERR_TOO_MANY_REDIRECTS) : 리디렉션이 너무 많습니다." – Raymonf

+0

@RBLXDev : 리다이렉트 루프를 만들었습니다. 그것들은 문제를 해결하는 것이 덜 쉽지만, 무엇이 도움이되는지, 지금 문제를 해결해야합니다. – hakre

답변

2

의견에서 언급 한대로 session_start()을 설정하십시오. 또한 ... 결국의 index.php에서

당신은 dictionary.php로 리디렉션 :

이 같은 조건 dictionary.php에서
if($_SESSION['auth'] == 0){ 
     header("Location: dictionary.php"); 
} 

당신의 index.php로 리디렉션 :

if($_SESSION['auth'] == 0){ 
    header("Location: index.php"); 
} 

따라서 사용자가 $_SESSION['auth'] == 0 인 상황에있을 때마다 무한 리디렉션이 발생합니다. 그걸 정리해야 해.

+0

감사합니다! 그래서 그게 문제였습니까? 나는 두 번 리디렉션 했어? – Raymonf

0

당신은 좀 더 편안 리디렉션 기능을해야한다 :

function site_redirect($location) 
{ 
    $headers = true; // set to false to disable for troubleshooting 

    $headers = $header && !headers_sent(); 
    if ($headers) { 
     header("Location: $location"); 
    } 
    printf(
     "<html><h1>Moved</h1><a href="%s">Moved to %1\$s</a></html>", 
     htmlspecialchars($location) 
    ); 
    exit(); 
} 

이는 더 나은 줄 응답 메시지를 리디렉션하지 않습니다, 또한 (디버깅 목적) 당신이 header(...); 라인을 비활성화 할 수 있습니다 (에 의해 예 당신의 브라우저가 더 이상 리다이렉트 루프를 입력하지 않도록 주석을 달아주세요.

그냥

site_redirect ("dictionary.php")로

header("Location: dictionary.php"); 

같은 통화를 대체;

가 더 이상 모든 스크립트에 하나의 header() 전화는 당신이 중앙 위치에서 리디렉션에 영향을 미칠 수 있도록 그 내부 site_redirect() 기능이되도록.