Jump to content

Login scrips Members & How to see if they are logged in


gnetuk

Recommended Posts

Hi, i have a site www.gnetuk.net where people can register and login,. thats all fine but what code can i use on my php pages to see whos logged in im sure its going to be a simple line of code or smthink but i am new to this stuff.

 

PLEASE HELP

 

<G>

Link to comment
Share on other sites

On the basis that you've used sessions for the login:

 

sessChk.inc.php:

<?php
if (!isset($_SESSION['sessName'])
{
header("Location: login.php");
exit;
}

$user['name'] = $_SESSION['sessName'];

 

This is how you would create that session in the login parser page:

session_start();
$_SESSION['sessName'] = $username;

 

Simples! By the way, replace $username with the username found in the login form i.e. $_POST['usr'], replace sessName in the sessions with whatever you like.

 

Hope that helps!

Link to comment
Share on other sites

Sorry I seemed to have answered the wrong question...

 

Ignace's table seems a bit much just to see how many people are online, but would work with the correct PHP code (something he didn't include).

 

What you could do is create a file somewhere in your root directory, something like userLog.dat or userLog.txt and then have some php code open the file, add an entry in a new line with an IP address (or a username from a session variable) and a timestamp on it. Then you'd parse the file finding every entry that is recent (your choice of time) and only allow one ip per entry in that timespan. Then you'd have the amount of users online recently. I would have to write this code up if you aren't sure how, or I could point you to a tutorial. The other downside is that you'd have to delete the contents of this file after a while or it would become huge.

Link to comment
Share on other sites

Sorry I seemed to have answered the wrong question...

 

Ignace's table seems a bit much just to see how many people are online, but would work with the correct PHP code (something he didn't include).

 

What you could do is create a file somewhere in your root directory, something like userLog.dat or userLog.txt and then have some php code open the file, add an entry in a new line with an IP address (or a username from a session variable) and a timestamp on it. Then you'd parse the file finding every entry that is recent (your choice of time) and only allow one ip per entry in that timespan. Then you'd have the amount of users online recently. I would have to write this code up if you aren't sure how, or I could point you to a tutorial. The other downside is that you'd have to delete the contents of this file after a while or it would become huge.

 

I don't need to provide the php code for this it's pretty obvious what and where you would use it. I only forgot to add the (obvious) update query which is called on every page request:

 

UPDATE users SET users_last_click_at = now() WHERE users_id = $id

 

The other downside is that you'd have to delete the contents of this file after a while or it would become huge.

 

Avoid the fuzz. On each page request will the users_last_click_at of a logged in user be updated. Using the above select query will give you all users who clicked between 5 minutes ago and now (someone who is actively clicking (and a member) at your website is IMO online). The only downside on my code is that it will only show online members and will ignore guests.

Link to comment
Share on other sites

Right then, Malevolence not to worry i think ignace has the answer im after not to be nasty but thanks for your post anyway.

 

ignace,  now i have the php code how do i print the users who are online on a page. Can you tell me the code.

 

Both of you are good and thx for posts and sorry for being a noob i can read php code and understand what it dose carnt write it yet :P

Link to comment
Share on other sites

CREATE TABLE users (
    users_last_click_at DATETIME,
);

 

SELECT * FROM users WHERE users_last_click_at BETWEEN now() - 300 AND now()

 

 

my sql comes up with a error when i tryy to run this i carnt create the table

Link to comment
Share on other sites

darn you ignace! foiled again!

 

Absolutely fair enough gnet, Ignace has clearly been learning php longer than me, kudos to him.

 

If you have a webpage you can add a mysql table on, create one - it could have failed because the table `users` has been taken.

 

-Try creating `activeUsers`, there will be 2 fields (as phpmyadmin and similar control panels require this number with the table name).

-Then you need to make field 1 "users_last_click_at" and make the field type 'DATETIME' under Date and Time in the list. -Then name the other field "users_id" and make this field a VARCHAR or SMALLINT (small integer). If varchar, make it about 6 chars wide.

 

Shove this at the top of every PHP page: (you may find it worth converting ALL your pages to .php by renaming the filetype as it could get sloppy)

<?php
include(sesschk.php);
include(dbconnect.php);

$q = "UPDATE users SET users_last_click_at = now() WHERE users_id = $id";
$q2 = "SELECT * FROM users WHERE users_last_click_at BETWEEN now() - 300 AND now()";
mysql_query($q) or die("MySQL Error: ".$mysql_error());
$r2 = mysql_query($q2);
$u = mysql_num_rows($r2);
?>

 

the includes at the top will include your 'logged-in' checker, so you may want to replace this line with your code or the file you use. The same goes for the database connection file, you may want to put your db connection script in a similarly named file or replace the line with your code. - It is a lot easier to put them in files to include to be honest.

 

To show how many users are logged in:

Users online: <?php echo $u; ?>

Link to comment
Share on other sites

CREATE TABLE users (
    users_last_click_at DATETIME,
);

 

SELECT * FROM users WHERE users_last_click_at BETWEEN now() - 300 AND now()

 

 

my sql comes up with a error when i tryy to run this i carnt create the table

 

Ofcourse this is just a part of a table. It indicates the fields you need in your table definition (not necessarily the same field definitions so you may need to adjust them).

Link to comment
Share on other sites

darn you ignace! foiled again!

 

Absolutely fair enough gnet, Ignace has clearly been learning php longer than me, kudos to him.

 

If you have a webpage you can add a mysql table on, create one - it could have failed because the table `users` has been taken.

 

-Try creating `activeUsers`, there will be 2 fields (as phpmyadmin and similar control panels require this number with the table name).

-Then you need to make field 1 "users_last_click_at" and make the field type 'DATETIME' under Date and Time in the list. -Then name the other field "users_id" and make this field a VARCHAR or SMALLINT (small integer). If varchar, make it about 6 chars wide.

 

Shove this at the top of every PHP page: (you may find it worth converting ALL your pages to .php by renaming the filetype as it could get sloppy)

<?php
include(sesschk.php);
include(dbconnect.php);

$q = "UPDATE users SET users_last_click_at = now() WHERE users_id = $id";
$q2 = "SELECT * FROM users WHERE users_last_click_at BETWEEN now() - 300 AND now()";
mysql_query($q) or die("MySQL Error: ".$mysql_error());
$r2 = mysql_query($q2);
$u = mysql_num_rows($r2);
?>

 

the includes at the top will include your 'logged-in' checker, so you may want to replace this line with your code or the file you use. The same goes for the database connection file, you may want to put your db connection script in a similarly named file or replace the line with your code. - It is a lot easier to put them in files to include to be honest.

 

To show how many users are logged in:

Users online: <?php echo $u; ?>

 

No Malevolence dont feel bad i carnt even create the table it comes up with mysql error even if i do change the table name

Link to comment
Share on other sites

CREATE TABLE users (
    users_last_click_at DATETIME
);

 

Note that after the datetime there was a comma. Next time post the error MySQL throws, it could help.

 

Try the above and it should create the table just fine.

Link to comment
Share on other sites

darn you ignace! foiled again!

 

Absolutely fair enough gnet, Ignace has clearly been learning php longer than me, kudos to him.

 

If you have a webpage you can add a mysql table on, create one - it could have failed because the table `users` has been taken.

 

-Try creating `activeUsers`, there will be 2 fields (as phpmyadmin and similar control panels require this number with the table name).

-Then you need to make field 1 "users_last_click_at" and make the field type 'DATETIME' under Date and Time in the list. -Then name the other field "users_id" and make this field a VARCHAR or SMALLINT (small integer). If varchar, make it about 6 chars wide.

 

Shove this at the top of every PHP page: (you may find it worth converting ALL your pages to .php by renaming the filetype as it could get sloppy)

<?php
include(sesschk.php);
include(dbconnect.php);

$q = "UPDATE users SET users_last_click_at = now() WHERE users_id = $id";
$q2 = "SELECT * FROM users WHERE users_last_click_at BETWEEN now() - 300 AND now()";
mysql_query($q) or die("MySQL Error: ".$mysql_error());
$r2 = mysql_query($q2);
$u = mysql_num_rows($r2);
?>

 

the includes at the top will include your 'logged-in' checker, so you may want to replace this line with your code or the file you use. The same goes for the database connection file, you may want to put your db connection script in a similarly named file or replace the line with your code. - It is a lot easier to put them in files to include to be honest.

 

To show how many users are logged in:

Users online: <?php echo $u; ?>

 

No Malevolence dont feel bad i carnt even create the table it comes up with mysql error even if i do change the table name

 

 

can you give me the exact sql command to run as i am a real noob to this

Link to comment
Share on other sites

CREATE TABLE users (
    users_last_click_at DATETIME
);

 

Note that after the datetime there was a comma. Next time post the error MySQL throws, it could help.

 

Try the above and it should create the table just fine.

 

 

sorry told you i was a noob see what a comma can do lol created table now ill let you know if i get it working thankx m8

Link to comment
Share on other sites

Absolutely fair enough gnet, Ignace has clearly been learning php longer than me, kudos to him.

 

My programming experience involves 4 till 5 years of PHP, SQL, VB.NET, Java, C++, Assembler and COBOL. None programming related experience involves project management.

 

Can you tell me the code.

 

Malevolence already did.

 

<?php
include('sesschk.php');
include('dbconnect.php');

$id = $_SESSION['users_id'];//or wherever you get the users id from
$q = "UPDATE users SET users_last_click_at = now() WHERE users_id = $id";//replace users and users_last_click_at with your table name and field name
$q2 = 'SELECT * FROM users WHERE users_last_click_at BETWEEN now() - 300 AND now()';//same here
mysql_query($q) or die('MySQL Error: '.$mysql_error());//remove the 'or die(..)' part once you are uploading it or replace it altogether with or trigger_error('MySQL Error: '.$mysql_error());
$r2 = mysql_query($q2);
$u = mysql_num_rows($r2);
?>

 

'or die()' is a bad practice and you should avoid it use 'or trigger_error()' instead. I'm not going into detail about why? Another member of these forums already did (Daniel0 'or die() must die', http://www.phpfreaks.com/blog/or-die-must-die)

Link to comment
Share on other sites

CREATE TABLE users (
    users_last_click_at DATETIME
);

 

Note that after the datetime there was a comma. Next time post the error MySQL throws, it could help.

 

Try the above and it should create the table just fine.

 

 

sorry told you i was a noob see what a comma can do lol created table now ill let you know if i get it working thankx mate

 

The table is worthless if you use it like that. I assumed you already had a users table (or whatever you want to call it) apparently you do not. The full SQL code would then be:

 

DROP TABLE IF EXISTS users;
CREATE TABLE users (
   users_id INTEGER NOT NULL AUTO_INCREMENT,
   users_username VARCHAR(16),
   users_password VARCHAR(40),
   users_email_address VARCHAR(96), #optional
   users_last_click_at DATETIME,
   users_last_login_at DATETIME, #optional
   users_registered_at DATETIME, #optional
   PRIMARY KEY (users_id)
);

 

The lines that have #optional in them can be removed if you don't want to store that information.

Link to comment
Share on other sites

yes i have a users table its all good and working so users who are registerd can login.

 

i wanted a seprate table for the logged in users that i can show users who are logged in.

 

e.g

 

users login and my table is updated with the name of the person who has logged in then i want a php page that prints the names of all members who have logged in.

 

i just dont know this is hard.

Link to comment
Share on other sites

this is all confusing for me. ok this is waht i learned/.

 

 

i need to create a table e.g usersgnetol = users gnet online

 

then i need to instert the name of the logged in user to gnetusersol

 

then i need to print on a page the logged in user

 

i have tryed all this and have had no luck.

Link to comment
Share on other sites

can you both help me as i an new,

 

this is waht i need

 

a database for logged in members called usersgnetol

 

then the code to put the username in from a database called usersgnet and a fild called name

 

then to print the name from usersgnetol of the people that are logged in.

 

you have all helped me but im still a bit lost.

Link to comment
Share on other sites

I never thought of those optional fields however for the current CMS I won't need all that data, but perhaps in my other CMS I will use it. Thanks for the info.

 

gnet, seeing as you seem so confused- and I'm not sure where you are at with coding php, can I have the database layout for the current table you have (as well as the table name) and all of your login script minus the db password and username? that way we can give you a code to work with straight off the bat rather than us giving you snippets all over the show.

Link to comment
Share on other sites

yes i have a users table its all good and working so users who are registerd can login.

 

i wanted a seprate table for the logged in users that i can show users who are logged in.

 

Don't. Stick to that one table and add the extra field

 

What is the sql code to add fields? sorry im new but know a little bit about php

Link to comment
Share on other sites

I never thought of those optional fields however for the current CMS I won't need all that data, but perhaps in my other CMS I will use it. Thanks for the info.

 

gnet, seeing as you seem so confused- and I'm not sure where you are at with coding php, can I have the database layout for the current table you have (as well as the table name) and all of your login script minus the db password and username? that way we can give you a code to work with straight off the bat rather than us giving you snippets all over the show.

 

Do you want me to e-mail you the files? can i have your email address please?

Link to comment
Share on other sites

darn you ignace! foiled again!

 

Absolutely fair enough gnet, Ignace has clearly been learning php longer than me, kudos to him.

 

If you have a webpage you can add a mysql table on, create one - it could have failed because the table `users` has been taken.

 

-Try creating `activeUsers`, there will be 2 fields (as phpmyadmin and similar control panels require this number with the table name).

-Then you need to make field 1 "users_last_click_at" and make the field type 'DATETIME' under Date and Time in the list. -Then name the other field "users_id" and make this field a VARCHAR or SMALLINT (small integer). If varchar, make it about 6 chars wide.

 

Shove this at the top of every PHP page: (you may find it worth converting ALL your pages to .php by renaming the filetype as it could get sloppy)

<?php
include(sesschk.php);
include(dbconnect.php);

$q = "UPDATE users SET users_last_click_at = now() WHERE users_id = $id";
$q2 = "SELECT * FROM users WHERE users_last_click_at BETWEEN now() - 300 AND now()";
mysql_query($q) or die("MySQL Error: ".$mysql_error());
$r2 = mysql_query($q2);
$u = mysql_num_rows($r2);
?>

 

the includes at the top will include your 'logged-in' checker, so you may want to replace this line with your code or the file you use. The same goes for the database connection file, you may want to put your db connection script in a similarly named file or replace the line with your code. - It is a lot easier to put them in files to include to be honest.

 

To show how many users are logged in:

Users online: <?php echo $u; ?>

 

i wanted to show the name of the logged in user not how many are logged in but mabee i could have both, names and the number of logged in users.

Link to comment
Share on other sites

darn you ignace! foiled again!

 

Absolutely fair enough gnet, Ignace has clearly been learning php longer than me, kudos to him.

 

If you have a webpage you can add a mysql table on, create one - it could have failed because the table `users` has been taken.

 

-Try creating `activeUsers`, there will be 2 fields (as phpmyadmin and similar control panels require this number with the table name).

-Then you need to make field 1 "users_last_click_at" and make the field type 'DATETIME' under Date and Time in the list. -Then name the other field "users_id" and make this field a VARCHAR or SMALLINT (small integer). If varchar, make it about 6 chars wide.

 

Shove this at the top of every PHP page: (you may find it worth converting ALL your pages to .php by renaming the filetype as it could get sloppy)

<?php
include(sesschk.php);
include(dbconnect.php);

$q = "UPDATE users SET users_last_click_at = now() WHERE users_id = $id";
$q2 = "SELECT * FROM users WHERE users_last_click_at BETWEEN now() - 300 AND now()";
mysql_query($q) or die("MySQL Error: ".$mysql_error());
$r2 = mysql_query($q2);
$u = mysql_num_rows($r2);
?>

 

the includes at the top will include your 'logged-in' checker, so you may want to replace this line with your code or the file you use. The same goes for the database connection file, you may want to put your db connection script in a similarly named file or replace the line with your code. - It is a lot easier to put them in files to include to be honest.

 

To show how many users are logged in:

Users online: <?php echo $u; ?>

 

i get a error with this the page wont load, programing error.

Link to comment
Share on other sites

  • 2 years later...

its me again gnetuk helo. (sprry to bring this up out of the woodwork)

 

Im getting better at reading php and tonight i will attempt to do the stuff on the prevouis posts.

 

Just wanted to know if you guys are still there coding lol. I had ago last night still no joy.

 

Here is what i got.

 

table called usersgnet with added user_last click field .

 

php code for the sessions (not working)

 

php code for the print of the online users.

 

I would also like there names listed but ill get to that when i get this thing working.

 

Dont worry ill sort it

 

Thanks guys <g>

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.