2013-04-24 5 views
0

여기 내 PHP 코드에 약간의 문제가 있습니다 ... 제발 도와 주실 수 있습니까? 문제는 내가 logout.php에서 세션을 설정 해제하고 세션을 파괴 할 때 처음 다른 페이지를로드 할 때 작동하지만 바로 뒤에서 새로 고침하면 세션이 다시 시작된다는 것입니다. 실제로는 그렇지 않습니다. 알다? 왜냐하면 나는 특정 이름의 세션을 찾을 나의 페이지가 있기 때문이다.PHP가 설정되지 않고 세션이 종료되었습니다.

Login.php :

<?php session_start(); 
//Get username and password 
$email = $_POST['email']; 
$password = $_POST['password']; 

//Sorting special characters away, with exception of "-" and "." 
stripslashes($email); 
$email = preg_replace('/[^[email protected]\.\-]/','', $email); 


//Getting the password from the database 
$link = mysqli_connect("****", "****", "****", "****"); 
if (mysqli_connect_errno($connect)) 
{ 
    echo "Connection Failed!"; 
    mysqli_close($connect); 
} 
$sql = "SELECT * FROM admins WHERE email = '". $email . "'"; 
if ($result = mysqli_query($link, $sql)) 
{ 
    while ($row = mysqli_fetch_row($result)) 
    { 
     $db_password = $row[2]; 
    } 
    mysqli_free_result($result); 

} 
mysqli_close($connect); 

//Compare DB-password to entered password 
if ($db_password == $password) 
{ 
    $_SESSION['admin'] = $email; 
    header("Location: ../index.php"); 
    exit(); 
} 
header("Location: index.php"); 
exit(); 
?> 

Logout.php :

if(!isset($_SESSION['admin'])) 
{ 
    header("Location: ../index.php"); 
    exit(); 
} 
else 
{ 
    session_unset(); 
    session_destroy(); 
    echo '<h1>You have been succesfully logged out!</h>'; 
    exit(); 
} 

Index.php는 :

if (isset($_SESSION['admin'])) 
{ 
    echo '<div id="admin"><br> 
    <h3>'.$_SESSION["admin"].'</h3> 
    <a href="http://www.mysite.com/admin"><span>Admin panel</span></a><br> 
    <a href="http://www.mysite.com/admin/logout.php"><span>Log out</span></a> 
    </div>'; 
} 

여기 내 코드입니다 그리고 예, 내 페이지의 각 위에 상단에 session_start()이 있습니다.

여러분이 index.php에서 볼 수 있듯이 $_SESSION['admin']이 설정된 경우 몇 가지 코드를 작성하고 싶습니다. 그리고 내가 logout.php에서 세션을 파괴하고 index.php로 간다면 처음 페이지를로드 할 때 작동합니다. 하지만 새로 고침하면 코드가 다시 나타납니다. 즉 세션이 다시 설정되어야합니다. 하지만 난 왜 몰라? 도와주세요!

편집 : 이제 login.php의 전체 코드를 입력했습니다. 나머지 두 페이지 중 나머지는 순수한 HTML입니다. 내가 게시 한 것은 모든 PHP 코드입니다!

+0

세션을 설정 해제 한 후 exit()를 호출하는 이유는 무엇입니까? 페이지로드를 끝내지 않는 이유는 무엇입니까? 헤더 위치를 설정 한 후에 exit를 호출해도 문제가 없지만 페이지를로드 할 때 페이지로드가 조기에 종료되는 이유는 무엇입니까? –

+0

로그 아웃 후 리 플래시 한 후 $ _SESSION [ 'admin']의 값은 얼마입니까? –

답변

1

PHPSESSID 쿠키 때문일 수 있습니다.

if ($db_password == $password) 

연결 세션을 생성, 설정 및 login.php에서을 index.php로 리디렉션됩니다 : 당신이 당신의 다음과 같은 조건 staisfies를 새로 고침하면 바로 브라우저

if(!isset($_SESSION['admin'])) 
{ 
    header("Location: ../index.php"); 
    exit(); 
} 
else 
{ 
    session_unset(); 
    session_destroy(); 
    setcookie('phpsessid','value',time()-1); 
    echo '<h1>You have been succesfully logged out!</h>'; 
    exit(); 
} 
+0

감사합니다, Pramod! 'setcookie ("PHPSESSID", "", 시간() - 3600, "/");를 추가하면 문제가 해결되었습니다! : D – user2140209

+0

@ user2140209 환영 –

0

에서 PHPSESSID 쿠키를 제거하여 그것을 시도 .

이 조건 변경 및 스크립트 작동

관련 문제