[PHP] Session help

Experienced Elementalist
Joined
Dec 3, 2007
Messages
262
Reaction score
15
hi guys, i need a code for this.

when login, it will store a time in the mssql server then it will record how many mins/hrs that user after logout. a

and also it is also monitor if user is it still online or offline that the user is not properly using the logout button.

thnx.
 
Forum Rules: http://forum.ragezone.com/f144/rules-general-info-180546/

Forum Rules said:
5. Don't just dump your problem and hope we sort it out for you!
Coders section is not a general 'I need something fixed so lets ask you guys to fix it for me' forum! If you have a specific problem, for instance a typecast that's giving you trouble or a regex that's not working correctly, feel free to ask. But do not dump here 50 lines of code with the message 'its not working, fix please', or expect us to write your scripts for you!
 
I don't like doings people work.. but I hope you learn from this.
PHP:
<?php
mysql_connect(localhost,root,password); //Localhost is default, root is also.
mysql_select_db(whateverdatabase);
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM users WHERE username='$username' and password='$password'"; // This is the query
$res = mysql_query($sql); // This establish's the query
$num = mysql_num_rows($res); // This sees how many values we got back
if($num=="1"){
session_start();
$_SESSION['username'];
}else{
echo "incorrect credintials";
}
?>

Not that this is not secure at all, and you would be hacked in an instant.
 
I don't like doings people work.. but I hope you learn from this.

<snip>

Not that this is not secure at all, and you would be hacked in an instant.

I am confused. You wish for the OP to learn, but you show him something that will damage his project.

PHP:
<?php
/**
 * Sanitize user input to prevent SQL injection.  Use this on ALL user input!
 * This function is from CodeIgniter.
 * @param string $data
 * @return string
 */
function mssql_escape_string($data) {
	if(!isset($data) or empty($data)) return '';
	if(is_numeric($data)) return $data;
	$non_displayables = array(
		'/%0[0-8bcef]/',			// url encoded 00-08, 11, 12, 14, 15
		'/%1[0-9a-f]/',				// url encoded 16-31
		'/[\x00-\x08]/',			// 00-08
		'/\x0b/',					// 11
		'/\x0c/',					// 12
		'/[\x0e-\x1f]/'				// 14-31
	);
	foreach($non_displayables as $regex)
		$data = preg_replace($regex,'',$data);
		$data = str_replace("'","''",$data);
	return $data;
}

//Store $_POST variables
$username = isset($_POST['username']) ? mssql_escape_string(trim($_POST['username'])) : '';
$password = isset($_POST['password']) ? mssql_escape_string(trim($_POST['password'])) : '';

if(isset($_POST) && !empty($_POST))
{
	//Connect to SQL
	mssql_connect('localhost', 'youraccount', 'yourpassword');

	$res = mssql_query("SELECT * FROM users WHERE username='{$username}' and password='{$password}'") or die("Query Failed");
	$num = ; // This sees how many values we got back
	if(mssql_num_rows($res))
	{
		//Store information + Log time
	}
	else
	{
		echo "Login Fail or whatever";
	}
}
?>

I am not that great of a PHP coder, but that should help you a bit.
 
I am confused. You wish for the OP to learn, but you show him something that will damage his project.

PHP:
<?php
/**
 * Sanitize user input to prevent SQL injection.  Use this on ALL user input!
 * This function is from CodeIgniter.
 * @param string $data
 * @return string
 */
function mssql_escape_string($data) {
	if(!isset($data) or empty($data)) return '';
	if(is_numeric($data)) return $data;
	$non_displayables = array(
		'/%0[0-8bcef]/',			// url encoded 00-08, 11, 12, 14, 15
		'/%1[0-9a-f]/',				// url encoded 16-31
		'/[\x00-\x08]/',			// 00-08
		'/\x0b/',					// 11
		'/\x0c/',					// 12
		'/[\x0e-\x1f]/'				// 14-31
	);
	foreach($non_displayables as $regex)
		$data = preg_replace($regex,'',$data);
		$data = str_replace("'","''",$data);
	return $data;
}

//Store $_POST variables
$username = isset($_POST['username']) ? mssql_escape_string(trim($_POST['username'])) : '';
$password = isset($_POST['password']) ? mssql_escape_string(trim($_POST['password'])) : '';

if(isset($_POST) && !empty($_POST))
{
	//Connect to SQL
	mssql_connect('localhost', 'youraccount', 'yourpassword');

	$res = mssql_query("SELECT * FROM users WHERE username='{$username}' and password='{$password}'") or die("Query Failed");
	$num = ; // This sees how many values we got back
	if(mssql_num_rows($res))
	{
		//Store information + Log time
	}
	else
	{
		echo "Login Fail or whatever";
	}
}
?>

I am not that great of a PHP coder, but that should help you a bit.

I know how it works, By learn he will study the security. You basicly just gave him a full script for him. Now he knows nothing, My whole point has been juked :/:
 
I didn't give him a full script. I gave him a snippet. Mine has security to check from and learn from, and the concept of how to run queries. Storing information and logging the time is not there. I also seemed to have left a variable from copy/pasting yours into Notepad++, oops.

Anyways, your code didn't help much either. He asked for one concering MSSQL, not MySQL. Although my script seemingly has it all there, depending on his PHP version, he'll have to convert to SqlSrv/ODBC, since PHP dropped support for MSSQL functions after PHP 5.3.
 
I had used an online/offline checker in one of my recent projects. Basically at the top of every page (in the included header), I updated the online status of the user to 1, and updated the user's last activity to the current timestamp. It also updated the online status for every user who's timestamp was more than x minutes ago, and set it to 0.

This way, active users will essentially be setting inactive users to offline, and they'll be setting themselves to online.

That was obviously just an extended measure in case users DIDN'T use the log out feature. When someone uses the log out, their online status is set to 0 right off the bat.

Does that make sense?
 
Back