RE: [PHP-WIN] RE: Sessions - Bug?
| From: | Jeroen ten Berge | Date: | Tue, 29 Aug 2000 06:52:14 +0000 |
| Subject: | RE: [PHP-WIN] RE: Sessions - Bug? | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-2431@lists.php.net to get a copy of this message | ||
Jep, that's possible too ;)
Here's some code :
//function returning the cookie value
function CheckCookie()
{
if (!$HTTP_COOKIE)
{
return "Nothing";
}
else
{
return $HTTP_COOKIE;
}
}
//function for cookie generation
function MakeCookie($user)
{
global $dbserver,$username,$password,$database;
srand((double)microtime()*1000000000);
$randval = rand();
$query="insert into owner.tblWebSession (CookID,Host,Fase,at1,ts)
values ($randval,'" . getenv("REMOTE_ADDR")
."','Main','$user',getdate())";
mssql_connect($dbserver,$username,$password) or
die(mssql_get_last_message());
mssql_select_db($database) or die(mssql_get_last_message());
$result = mssql_query($query) or die(mssql_get_last_message());
mssql_close();
setcookie("MYPROG",$randval,time()+3600,"/");
return $randval;
}
//function for ending a session
function CloseSession($CookID)
{
global $dbserver,$username,$password,$database;
$query="delete from owner.tblWebSession where CookID=\"$CookID\"";
mssql_connect($dbserver,$username,$password) or
die(mssql_get_last_message());
mssql_select_db($database) or die(mssql_get_last_message());
$result = mssql_query($query) or die(mssql_get_last_message());
mssql_close();
return "Succes";
}
/*Validate user and put settings in a cookie
Returns "Logon succesfull" if validated OK, otherwise it returns an
appropiate error */
function ValidateUP($UserCode,$PassWd)
{
global $dbserver, $username, $password, $database, $cookie_life;
$UserCode = addslashes($UserCode);
$PassWd = addslashes($PassWd);
// Make sure that UserCode and PassWd aren't empty
If (!$UserCode)
{
return ("No user name entered");
exit;
}
else
{
If (!$PassWd)
{
return ("No password entered");
exit;
}
else
{
$query="SELECT * FROM owner.vwCustomer WHERE CustomerCode = \"" .
$UserCode . "\";";
mssql_connect($dbserver,$username,$password) or die("Database is
down");
mssql_select_db($database) or die("Database is down");
$result = mssql_query($query) or die("Query failed");
$row = mssql_fetch_array($result);
mssql_close();
if (!$row)
{
return ("Not recognised");
exit;
}
if ($row['CustomerPassword'] != $PassWd)
{
return ("Wrong password");
exit;
}
else
{
return ("Logon succesfull");
}
}
}
}
function GetSessionAction($ID)
{
if (!$ID)
{
return "LogOut";
exit;
}
else
{
global $dbserver, $username, $password, $database;
$query="SELECT Fase FROM owner.tblWebSession WHERE CookID = \"" . $ID
. "\"";
mssql_connect($dbserver,$username,$password) or die("Database is
down");
mssql_select_db($database) or die("Database is down");
$result = mssql_query($query) or die("Query failed");
$row = mssql_fetch_array($result);
mssql_close();
$tmp=$row['Fase'];
return $tmp;
}
}
if (($REQUEST_METHOD=='POST'))
{
if (isset ($HTTP_POST_VARS["btnLogin"]))
{
if ($txtLoginName && $txtPassword)
{
include ("main.php");
$strTemp = ValidateUP ($txtLoginName,$txtPassword);
if ($strTemp<>"Logon succesfull")
{
echo "<html><head><meta http-equiv=\"Content-Language\"
content=\"en-us\">";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html;
charset=windows-1252\">";
echo "<title>$strTemp!</title>";
echo "</head><body>";
echo "<p align=\"center\"><img border=\"0\"
src=\"images/logo.gif\"
width=\"307\" height=\"66\"></p>";
echo "<div align=\"center\">";
echo "<FONT color=red>This Login ID is not recognised or you do not
have permission to log on!</FONT>";
echo "</center></div></FORM></body></html>";
exit;
}
else
{
if (!$HTTP_COOKIE_VARS["MYPROG"])
{
MakeCookie($txtLoginName);
}
echo "<html><head><meta http-equiv=\"Content-Language\"
content=\"en-us\">";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html;
charset=windows-1252\">";
echo "<title>$strTemp!</title>";
echo "</head><body>";
echo "<p align=\"center\"><img border=\"0\"
src=\"images/logo.gif\"
width=\"307\" height=\"66\"></p>";
echo "<FORM METHOD=\"POST\" NAME=\"frmMenuItem1\"
ACTION=\"$PHP_SELF\">";
echo "<p><center><input type=\"submit\" size=30
value=\"Menuitem1\"
name=\"btnMenuItem1\"></p>";
echo "</FORM><FORM METHOD=\"POST\" NAME=\"frmMenuItem2\"
ACTION=\"$PHP_SELF\">";
echo "<p><center><input type=\"submit\" size=30 value=\"Menu
item 2\"
name=\"btnMenuItem2\"></p>";
echo "</FORM>";
echo "<FORM METHOD=\"POST\" NAME=\"frmLogout\"
ACTION=\"$PHP_SELF\">";
echo "<p><center><input type=\"submit\" size=30 value=\"Log
out\"
name=\"btnLogout\"></p>";
echo "</center></div></FORM></body></html>";
exit;
}
}
}
else
{
CloseSession($HTTP_COOKIE_VARS["MYPROG"]);
setcookie("MYPROG","",time(),"/");
}
}
Hope this helps ;)
Remember for every page load to check the cookie when nessecary, you can
lookup your variables with querie's.
Regards,
Jeroen.
-----Original Message-----
From: James Duncan [mailto:jd@jamesduncan.co.uk]
Sent: Monday, August 28, 2000 11:05 PM
To: Jeroen ten Berge
Subject: RE: [PHP-WIN] RE: Sessions - Bug?
I've changed my mind. Can you send me a sample of your code as it seems a
very simple and effective solution!
Thanks
James
PS: If you wanted instead of storing the ID in a cookie you could pass it
via the URL for every page (encase client has cookie's disabled)?
-----Original Message-----
From: Jeroen ten Berge [mailto:j.ten.berge@getexo.nl]
Sent: 28 August 2000 09:46
To: qablane@surfaid.org; php-windows@lists.php.net
Subject: RE: [PHP-WIN] RE: Sessions - Bug?
This is how I do it under PHP 3.0.16 :
On login form submitmake a cookie with an ID, store this ID in a table on
the db with a timestamp, in this table you add a few varchar's, longint's,
boolean and datetime fields; Now in these fields you store your variables;
And for every page you check this ID and lookup the value's (in
$HTTP_COOKIE_VARS["<yourIDname>"])
At the logout procedure you just delete the row from the table.
Works like a glance for me ;)
Regards,
Jeroen.
-----Original Message-----
From: Quintin Blane [mailto:qablane@surfaid.org]
Sent: Monday, August 28, 2000 10:06
To: php-windows@lists.php.net
Subject: [PHP-WIN] RE: Sessions - Bug?
> So this is either a) A bug, b) I'm going mad or c) I'm being really stupid
and messing up in a big way!
>James
I have no answers but I have given up attempting to use the session
functions under Windows as a way to control login/logout, as they simply
behave as the manual leads me to expect.
Quintin
--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-windows-unsubscribe@lists.php.net
For additional commands, e-mail: php-windows-help@lists.php.net
To contact the list administrators, e-mail: php-list-admin@lists.php.net