Reputation: 83
So I found/made an "online users" page that shows the users online as well as the visitors, when I log in it shows me as a visitor. I think my sessions aren't working correctly, I've had a lot of problems with database selections too (I'm supposed to get results according to the username, but it doesn't work). I also can't access pages that are set to only allow logged in users.
Here's the code that creates the session name that I call up later
$_SESSION['userlogin']=$username;
Here's the code that prevents users from accessing the page if they aren't logged in, but I get the "You must be logged in" even when I am logged in!!
<?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");
?>
And here's the entire users online code
<?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
?>
Here's the weird error I get on the users online page too - "127.0.0.1-vst-^^1411198259"
Any ideas? You can test this out yourself as well as my website is up (www.velrania.com), try to ignore all the other errors too :P
Upvotes: 0
Views: 61
Reputation: 2880
Start the session in first row...
session_start();
include("connect.php");
include("header.php");
include('userson.txt');
Upvotes: 1