所以我找到了一个"在线用户"显示用户在线以及访问者的页面,当我登录时显示我作为访问者。我认为我的会话没有正常工作,我的数据库选择也遇到了很多问题(我应该根据用户名获得结果,但它不起作用) 。我也无法访问设置为仅允许登录用户的页面。
这是创建我稍后调用的会话名称的代码
$_SESSION['userlogin']=$username;
这里是阻止用户访问该页面的代码,如果他们没有登录,但我得到了#34;您必须登录"即使我登录了!!
<?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
?>
这是我在用户在线页面上发现的奇怪错误 - &#34; 127.0.0.1-vst - ^^ 1411198259&#34;
有什么想法吗?您可以自己测试一下以及我的网站(www.velrania.com),尝试忽略所有其他错误:P
答案 0 :(得分:1)
在第一行开始会话......
session_start();
include("connect.php");
include("header.php");
include('userson.txt');