Scripts

PHP/MYSQL Inlog functie

Inlog functie met database backend, bewaart lokaal cookie met unieke md5 hash. Je gebruikt het als volgt: // header (bovenkant) van je layout // footer (onderkant) van je layout

phpmysql-inlog-functie
<?php

/*
Made by : Robin Hozee
License	: OGL
Source	: Public


$website	='';	//Website name
$domain		='';	//Domain
$path		='';	//path of the website on domain
$expire		='';	//Time for cookie to expire in seconds

Supports the following actions
login   : Form action target.php?action=login
logout	: Option when logged in, url target.php?action=logout
and continued renewal of cookie + cookiehash when logged in.

$name and $pass should be extracted from the loginform

This function assumes that there is already a connection to the database.

Required SQL;
CREATE TABLE `users` (
  `id` int(4) NOT NULL auto_increment,
  `name` varchar(20) NOT NULL default '',
  `password` varchar(32) NOT NULL default '',
  `timehash` varchar(32) NOT NULL default '',
  `ip` varchar(20) NOT NULL default '',
  PRIMARY KEY  (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;
*/


function usersession($website,$domain,$path,$expire)
		{
		global $action;
		global $name;
		global $pass;
		if($action == "login")
				{
                $selectsql = 'SELECT * FROM users WHERE name="'.$name.'" AND password="'.md5($pass).'"';
				$rows = mysql_num_rows(mysql_query($selectsql));
				if($rows == 1) 
					{							$timehash = md5(time());
					$update = 'UPDATE users SET timehash="'.$timehash.'", ip="'.$_SERVER[REMOTE_ADDR].'" WHERE name="'.$name.'"';
					mysql_query($update);
					setcookie($website, $timehash, time()+$expire, $path, $domain, "0");
					
					}
				if($rows == 0)
					{
                    
					}
				echo "<script>location.href='/'</script>";
				}

		if($action == "logout")
			{ 
			$update = 'UPDATE users SET timehash="logout" WHERE ip="'.$_SERVER[REMOTE_ADDR].'"';
			mysql_query($update);
			}

		else
			{
			$selectsql = 'SELECT * FROM users WHERE timehash="'.$_COOKIE[$website].'" AND ip="'.$_SERVER[REMOTE_ADDR].'"';
			$select = mysql_query($selectsql);
			$rows = mysql_num_rows($select);
			if($rows > 0) 
				{
				$user = mysql_fetch_array($select);
				$timehash = md5(time());
				$update = 'UPDATE users SET timehash="'.$timehash.'" WHERE ip="'.$_SERVER[REMOTE_ADDR].'"';
				mysql_query($update);
				setcookie($website, $timehash, time()+$expire, $path, $domain, "0"); 

				return $user;
				}
			}

		}

?>

Reacties

0
Nog geen reacties.