Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

php 쿠키의 생성 #159

Closed
tomorrow9913 opened this issue Jan 17, 2018 · 3 comments
Closed

php 쿠키의 생성 #159

tomorrow9913 opened this issue Jan 17, 2018 · 3 comments

Comments

@tomorrow9913
Copy link

tomorrow9913 commented Jan 17, 2018

쿠키생성에서 오류가 있고 숫자가 정상적으로 올라가지 않네요... 해결 방법을 알 수 있을까요?

파일 권한은 777로 주었습니다

출력되는 내용
Warning: Cannot modify header information - headers already sent by (output started at /home/tomorrow9901/www/counter.php:2) in /home/tomorrow9901/www/counter.php on line 26

count.txt의 내용
2018-01-17,1,1

코드

<meta charset="utf-8">
<?
if ( !file_exists ( "count.txt") )
{
    $fp = fopen("count.txt", "w+") ;
    fclose($fp) ;
}
$count = file("count.txt") ;
$count = chop($count[0]) ;
$countdata = split(",",$count[0]);
$date =  strtotime($countdata[0]);
$daycount = (int)$countdata[1];
$counta = (int)$countdata[2];
$today = date("Y-m-d");
if ( !$_COOKIE["ip"] ){//쿠키가 없으면
  if($date == $today){//오늘 날짜와 txt의 날짜를 비교하여 같으면
    $daycount = $daycount + 1;// countday를 1 올린다.
  } else {//다르면
      $date = $today;//date를 오늘 날짜로 바꾼다.
      $daycount = 1 ;//daycount를 1로 한다.
    }
  $counta = $counta+1 ;
  $fp = fopen("count.txt", "w") ;
  fwrite($fp, "$date,$daycount,$counta") ;
  fclose($fp) ;
  SetCookie("ip", $REMOTE_ADDR, time() + 3600) ;//쿠키를 추가한다
}
echo $date." 의 방문자 수 : ".$daycount."<br>전체 방문자 수 : ".$counta;
?>

image

@ghdalsrldi
Copy link
Collaborator

set_cookie 함수는 서버에서 쿠키를 생성해 헤더에 쿠키를 담아 응답합니다.

하지만 위의 코드를 살펴보니 쿠키 생성 이전에 출력이 일어납니다

<meta charset="utf-8">

이 부분을

echo $date." 의 방문자 수 : ".$daycount."<br>전체 방문자 수 : ".$counta;

여기 위로 올려 주시면 해결 될 것이라 생각 됩니다.

@tomorrow9913
Copy link
Author

tomorrow9913 commented Jan 18, 2018

우선 친절한 답변 감사합니다.

답변해 주신대로 코드를 수정해 보았습니다.
쿠키는 정상적으로 생성되었습니다.

if ( !file_exists ( "count.txt") )
{
    $fp = fopen("count.txt", "w+") ;
    fclose($fp) ;
}
$count = file("count.txt") ;
$count = chop($count[0]) ;
$countdata = explode("::", $count[0]);
$date = $countdata[0];
$daycount = (int)$countdata[1];
$counta = (int)$count[2];
$today = date("Y-m-d");
if ( !$_COOKIE["ip"] ){//쿠키가 없으면
  if($date == $today){//오늘 날짜와 txt의 날짜를 비교하고 같으면
    $daycount++ ;// countday를 1 올린다.
  } else {//다르면
      $date = $today;//date를 오늘 날짜로 바꾼다.
      $daycount = 1 ;//daycount를 1로 한다.
    }
  $counta++ ;
  $fp = fopen("count.txt", "w") ;
  fwrite($fp, "$date::$daycount::$counta") ;
  fclose($fp) ;
  SetCookie("ip", $REMOTE_ADDR, time() + 7200) ;//쿠키를 추가한다
}
echo '<meta charset="utf-8">';
echo "<font color='red'><strong>".$date."</strong></font> 의 방문자 수 : <font color='red'><strong>".$daycount."</strong></font><br>전체 방문자 수 :<font color='red'><strong> ".$counta."</strong></font>";

그러나 쿠키를 삭제한 상태에서도 변수 counta 와 daycount가 계속 값이 변하지 않습니다.
image
split가 제대로 작동하지 않는 듯 합니다. 그래서 explode로 바꾸어서 다시 시도해 보았지만 결과는 똑같았습니다. 한번 더 도와주실 수 있겠습니까??

$count(file의 내용)의 값 : 2018-01-18::5::13
$date의(file의 날짜) 값 : 2
$daycount의(오늘의 방문자 수) 값 : 0
$counta의(전체 방문자 수) 값 : 1

위의 내용은 아래의 코드를 출력해서 얻은 내용입니다.

echo "<br><br>변수 today와 date의 비교결과 : ";var_dump((bool) $date == $today);
echo "<br>\$count(file의 내용)의 값 : ".$count."<br>\$date의(file의 날짜) 값 : ".$date."<br>\$daycount의(오늘의 방문자 수) 값 : ".$daycount."<br>\$counta의(전체 방문자 수) 값 : ".$counta."<br><br>";

다들 도와주셔서 마무리 할 수 있었습니다.
도와주셔서 감사합니다

if ( !file_exists ( "count.txt") )
{
    $fp = fopen("count.txt", "w+") ;
    fclose($fp) ;
}
$count = file("count.txt") ;
$count[0] = chop($count[0]) ;
$countdata = split("::", $count[0]);
$date = $countdata[0];
$daycount = (int)$countdata[1];
$counta = (int)$countdata[2];
$today = date("Y-m-d");
if ( !$_COOKIE["ip"] ){//쿠키가 없으면
  if($date == $today){//오늘 날짜와 txt의 날짜를 비교하고 같으면
    $daycount++ ;// countday를 1 올린다.
  } else {//다르면
      $date = $today;//date를 오늘 날짜로 바꾼다.
      $daycount = 1 ;//daycount를 1로 한다.
    }
  $counta++ ;
  $fp = fopen("count.txt", "w") ;
  fwrite($fp, "$date::$daycount::$counta") ;
  fclose($fp) ;
  SetCookie("ip", $REMOTE_ADDR, time() + 7200) ;//쿠키를 추가한다
}
echo '<meta charset="utf-8">';
echo "<font color='red'><strong>".$date."</strong></font> 의 방문자 수 : <font color='red'><strong>".$daycount."</strong></font><br>전체 방문자 수 :<font color='red'><strong> ".$counta."</strong></font>";

이렇게 하니 잘 작동하네요.
image

다시 한번 머리숙여 감사드립니다.

@azaraks
Copy link

azaraks commented Jan 18, 2018

split이 문제라고 생각한다면 $countdatavar_dump하지 않는 이유는 무엇인가요?
그리고 $count = chop($count[0]);는 다음 줄의 내용으로 봐서, $count[0] = chop($count[0]);일 듯 합니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants