note 5945 modified in HTTP authentication with PHP by ronabop

From: Date: Sat, 23 Sep 2000 18:01:57 +0000
Subject: note 5945 modified in HTTP authentication with PHP by ronabop
Groups: php.notes 
Request: Send a blank email to php-notes+get-5439@lists.php.net to get a copy of this message
Here is a code for the public sites enabling both logout bottom and timeout using php+mysql. Working for both browsers. The part "required" for each user protected page: <? function auth () { Header("WWW-Authenticate: Basic realm=\"ArmFN public site\""); Header("HTTP/1.0 401 Unauthorized"); echo "You have to authentificate yourself first \n"; exit; } mysql_connect("localhost","train","") or die("Unable to connect to SQL server"); mysql_select_db( "train") or die( "Unable to select database"); if(!isset($PHP_AUTH_USER)) { $timeout = mktime(date(G),date(i)+10,0,date("m"),date("d"),date("Y")); mysql_query("update users set login='$timeout' where id='$user' and pasw='$pass'") or die("k"); auth(); } else { $pass = $PHP_AUTH_PW; $user = $PHP_AUTH_USER; $nowtime = mktime(date(G),date(i),0,date("m"),date("d"),date("Y")); $quer2 = mysql_query("select * from users where id='$user' and pasw='$pass' and login > '$nowtime'") or die("kuk2"); if (mysql_num_rows($quer2) == "0") { $timeout = mktime(date(G),date(i)+10,0,date("m"),date("d"),date("Y")); mysql_query("update users set login='$timeout' where id='$user' and pasw='$pass'") or die("k"); auth(); } } ?> You can have a "logout" bottom with hidden $go="logout" form element and then have somewhere this part: if ($do == "logout") { mysql_connect("localhost","train","") or die("Unable to connect to SQL server"); mysql_select_db( "train") or die( "Unable to select database"); mysql_query("update users set login=0 where id='$PHP_AUTH_USER' and pasw='$PHP_AUTH_PW'") or die("k"); } http://www.php.net/manual/http-authentication-with-php.php

« previous php.notes (#5439) next »