2014-09-20 2 views
0

그래서 온라인 사용자와 방문자를 보여주는 "온라인 사용자"페이지를 만들었습니다. 로그인하면 방문자로 표시됩니다. 세션이 제대로 작동하지 않는다고 생각합니다. 데이터베이스 선택에 많은 문제가 있습니다 (사용자 이름에 따라 결과를 얻으려고하는데 작동하지 않습니다). 또한 로그인 한 사용자 만 허용하도록 설정된 페이지에는 액세스 할 수 없습니다. 여기 세션이 시작되지 않습니까? + 이상한 오류 메시지

는 나중에

$_SESSION['userlogin']=$username; 

여기가 로그인하지 않은 경우 페이지에 액세스 할 수 없도록 코드의 호출 세션 이름을 생성하는 코드이지만, 나는 "당신은해야 얻을 로그인 한 상태에서도 로그인했습니다. "127.0.0.1 - VST - - ^^ 1411198259"

모든

<?php 
include("header.php"); 
if(isset($_SESSION['userlogin'])){ 
echo "You must be logged in to view this page!"; 
}else{ 

echo "Success, figured it out eh?"; 
?> 

<?php 
} 
include("footer.php"); 
?> 

그리고 여기가 여기가 너무 사용자 온라인 페이지에서 얻을 이상한 오류의 전체 사용자 온라인 코드

<?php 
include("connect.php"); 
include("header.php"); 
include('userson.txt'); 
if(isset($_SESSION)) session_start();  // start Session, if not already started 

$filetxt = 'userson.txt'; // the file in which the online users /visitors are stored 
$timeon = 120;    // number of secconds to keep a user online 
$sep = '^^';    // characters used to separate the user name and date-time 
$vst_id = '-vst-';   // an identifier to know that it is a visitor, not logged user 

/* 
If you have an user registration script, 
replace $_SESSION['nume'] with the variable in which the user name is stored. 
*/ 

// get the user name if it is logged, or the visitors IP (and add the identifier) 
$uvon = isset($_SESSION['userlogin']) ? $_SESSION['userlogin'] : $_SERVER['SERVER_ADDR']. $vst_id; 

$rgxvst = '/^([0-9\.]*)'. $vst_id. '/i';   // regexp to recognize the line with visitors 
$nrvst = 0;          // to store the number of visitors 

// sets the row with the current user /visitor that must be added in $filetxt (and current timestamp) 
$addrow[] = $uvon. $sep. time(); 

// check if the file from $filetxt exists and is writable 
if(is_writable($filetxt)) { 
// get into an array the lines added in $filetxt 
$ar_rows = file($filetxt, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); 
$nrrows = count($ar_rows);   // number of rows 

// if there is at least one line, parse the $ar_rows array 
if($nrrows>0) { 
for($i=0; $i<$nrrows; $i++) { 
    // get each line and separate the user /visitor and the timestamp 
    $ar_line = explode($sep, $ar_rows[$i]); 

    // add in $addrow array the records in last $timeon seconds 
    if($ar_line[0]!=$uvon && (intval($ar_line[1])+$timeon)>=time()) { 
    $addrow[] = $ar_rows[$i]; 
     } 
    } 
    } 
} 

$nruvon = count($addrow);     // total online 
$usron = '';         // to store the name of logged users 
// traverse $addrow to get the number of visitors and users 
for($i=0; $i<$nruvon; $i++) { 
if(preg_match($rgxvst, $addrow[$i])) $nrvst++;  // increment the visitors 
else { 
// gets and stores the user's name 
$ar_usron = explode($sep, $addrow[$i]); 
$usron .= '<br/> - <i>'. $ar_usron[0]. '</i>'; 
} 
} 
$nrusr = $nruvon - $nrvst;    // gets the users (total - visitors) 

// the HTML code with data to be displayed 
$reout = '<div id="uvon"><h4>Online: '. $nruvon. '</h4>Visitors: '. $nrvst. '<br/>Users: '. $nrusr.  $usron. '</div>'; 

// write data in $filetxt 
if(!file_put_contents($filetxt, implode("\n", $addrow))) $reout = 'Error: Recording file not exists,  or is not writable'; 

// if access from <script>, with GET 'uvon=showon', adds the string to return into a JS statement 
// in this way the script can also be included in .html files 
if(isset($_GET['uvon']) && $_GET['uvon']=='showon') $reout = "document.write('$reout');"; 

echo $reout;    // output /display the result 
?> 

입니다 아이디어? 내 웹 사이트가 그대로 당신은뿐만 아니라 자신이을 테스트 할 수 있습니다 (www.velrania.com), 너무 다른 모든 오류를 무시하려고 : P

+0

사용자가 로그인했는지 확인하는 코드는 잘못된 것 같습니다. if (isset ($ _ SESSION [ 'userlogin'])) –

답변

1

시작 첫 번째 행에서 세션 ...

session_start(); 
include("connect.php"); 
include("header.php"); 
include('userson.txt'); 
+0

그리고'if (isset ($ _ SESSION)) session_start();'를 제거해야합니다. – GhostGambler

+0

아니면 그냥 "!" 발급 전에. if (! isset ($ _SESSION)) {session_start(); } –

+1

그는 먼저 두 경우 모두 세션을 시작해야하며, 그 다음에는 isset 여부를 확인해야합니다. – alalp

관련 문제