2012-12-18 4 views
-1

나는 사용자가 전자 메일과 암호를 입력 할 수있는 signin.php이라는 로그인 페이지를 가지고 있습니다. 제출 버튼을 클릭하면 페이지가 connection_validate.php으로 안내됩니다. 이 페이지는 데이터베이스로 사용자가 입력 한 데이터의 유효성을 검사합니다. 등록 된 사용자 인 경우 페이지는 calendar.php으로 연결됩니다. 입력 한 데이터가 올바르지 않으면 signin.php으로 리디렉션되어야합니다. 입력 한 데이터가 잘못된 경우,이 같은 쿠키를 배치 한 :프로그래밍 방식으로 쿠키 지우기

<?php 
include("include/minfooter.php"); 
if(isset($_COOKIE["message"])) 
{ 
    if(!($_COOKIE["message"]==" ")) 
    { 
     echo "<script> 
    alert('Incorrect login information'); 
    </script>"; 
    setcookie("message"," ",time()-3600); 
    } 
} 
?> 

: 로그인 정보가 이런 잘못된 경우

//action to be done if e mail id and password matches with database records    
if(mysql_num_rows($result)>0) 
{ 
    header('location:calendar.php'); 
} 
//action to be done if e mail id and password does not matches with database records 
else 
{ 
    setcookie('message','incorrect login data'); 
    header('location:signin.php'); 
} 

이 signin.php에서, 내가 경고를 표시하는 코드를 작성했습니다 내 문제는 로그인 오류 데이터를 한 번 입력하면 로그인 페이지를로드 할 때마다 알림이 표시된다는 것입니다. calendar.php에서 signin.php로 돌아 가기 버튼을 누르면 경고가 표시되기 시작합니다. 나는 그 문제가 쿠키에 있다는 것을 이해했다. 쿠키가 삭제되지 않았습니다. 이 문제를 어떻게 해결할 수 있습니까?

답변

1

업데이트합니다 signin.php 당신이 세션 당신이 $ _SESSION 변수 대신 쿠키 값을 사용할 수 있습니다 사용하는 경우

<?php 
    include("include/minfooter.php"); 
    if (isset($_COOKIE["message"])) 
    { 

     echo "<script> 
       var delete_cookie = function(name) { 
        document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;'; 
       }; 
       var msg = '" . $_COOKIE["message"] . "'; 
       if (msg != '') 
        alert('Incorrect login information'); 
       delete_cookie('message'); 
      </script>"; 
    } 
?> 
+0

이 잘못된 것은 setcookie()는 이미 콘텐츠를 브라우저에 출력 한 후에 설정할 수없는 HTTP 헤더를 설정합니다. 또한 구문이 잘못되었습니다. –

+0

내 대답이 업데이트되었습니다. 지금은 100 %가 올바른 것입니다. – user1911857

+0

나는 75 % 정확하다고 말하고 싶습니다. 당신은 if (isset ($ _ COOKIE [ "message"]))를 두 번 가지고 있지만 여전히 올바른 접근법은 아닙니다. –

1

을 다음과 같이. 또한 setcookie()는 내용을 보내기 전에 보내야하는 HTTP 헤더를 보내기 때문에 setcookie()를 사용할 수 없습니다.

session_start(); 
//action to be done if email id and password matches with database records 
if (mysql_num_rows($result) > 0) 
{ 
    header('Location: calendar.php'); 
    exit; 
} 
//action to be done if email id and password does not matches with database records 
else 
{ 
    $_SESSION['message'] = 'incorrect login data'; 
    header('Location: signin.php'); 
    exit; 
} 

은 그런 : 당신이 원하는 경우

<?php 

session_start(); 
include("include/minfooter.php"); 

if (!empty($_SESSION['message'])) 
{ 
    echo "<script>alert('" . $_SESSION["message"] . "');</script>"; 
    $_SESSION['message'] = ''; 
} 

?> 
0

좋아 어쩌면 그 [ '메시지']를 $ _SESSION 배열을 인덱스를 사용하기위한 세션을 사용하는 것이 더 좋습니다, 다음 정리는, 쿠키가 사용되어야한다 사용자가 귀하의 페이지를 벗어나면 몇 가지 정보를 참조하십시오. 쿠키를 사용하는 코드를 만들었지 만 세션 사용을 고려해보십시오.

<?php include("include/minfooter.php"); 
     if(isset($_COOKIE["message"]) && !empty($_COOKIE["message"]) 
     { 
       echo "<script> 
        var msg = '<?php echo $_COOKIE["message"];?>'; 
        if (msg != "") 
        alert('Incorrect login information'); 
        </script>"; 
       unset($_COOKIE["message"]); 
     } 
?> 
관련 문제