setcookie() when logging in and logging out

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
soshea
Forum Commoner
Posts: 31
Joined: Tue Nov 12, 2002 11:22 pm
Location: Idaho
Contact:

setcookie() when logging in and logging out

Post by soshea »

hey all,
i am writing a login page for an are that utilizes HTTPS. for browsers for IE6.0 and netscape7.0 the page works fine. netscape 6.0 and below plus IE5 and below has problems though. they do not want to set cookies so the login page is continuously showing up. (i know this could be their own browser settings to accept cookies but since i am new to php having some one else look at my code is a good thing). also for netscape 7 and mozzilla 1.1 when they logout the cookie is not reset to "".
any ideas or suggestions on how to improve this or if there are some quirks about PHP that i should know about, please advise!!!!
LOGIN

Code: Select all

<?php
<?php
if ($_SERVER['HTTPS'] !== 'on') {
	die("Please utilize HTTPS://www.testsite.com/Admin/");
}
?>
<?php
if (($userid) && ($password)) {
//query the data
$construct = "dbname=UerDb user=postgres";
$dbh = pg_connect($construct);
	if ($dbh){
		$sql = pg_exec($dbh, "select userid, password from hammersecurity where userid='$userid' and password='$password'");
		$rows = pg_numrows($sql);
		pg_close($dbh);
		if ($rows > 0) {
		for ($i = 0; $i < $rows; $i++) {
		$data = pg_fetch_array($sql, $i);
		$usercookie = $data["userid"];
		setcookie("usercookie","$usercookie",time()+3600,"/Admin/","TestSite.com",1);
		exit (header ('Location:../Admin/'));	
		}
		} else {
		echo "<table align=center valign=middle height=100%><tr><td valign=middle align=center>A Valid UserID and Password must be entered</td></tr></table>";
		}
	} else {
		echo "no connection has been established";
		exit;
	}
}
?>	

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
 <HEAD>
  <TITLE>LOGIN</TITLE>
<link REL="stylesheet" type="text/css" Href="../styles/Hammer.css">
</HEAD>
 <BODY bgcolor="#FFFFFF">
<table align="center" valign="middle" border="0" height="100%" width="100%">
	<tr>
		<td class="Main1" align="center" valign="middle">
		<form action="<?php echo $php_self ?>" name="login" method="post">
		<table align="center" border="0" bgcolor="#DDDDDD" cellpadding="2" cellspacing="2">
		<tr>
		<td class="Main1" colspan="2">Enter your ID and Password to sign in
		</tr>
		<tr>
		<td class="Main1" width="50">User ID:</td><td><input type="text" name="userid" size="20"></td>
		</tr>
		<tr>
		<td class="Main1">Password:<td><input type="password" name="password" size="20"></td>	
		</tr>
		<tr>
		<td class="Main1" colspan="2"><input type="Submit" value="Sign In">	
		</tr>
		</table>
		</form>
		<br>
		</td>
	</tr>
</table>

?>
LOGOUT

Code: Select all

<?php
<?php
setcookie("userid","",time()-3600,"/Admin/","TestSite.com",1);
?>
<table align="center" valign="middle" border="0" height="100%" width="100%">
	<tr>
		<td class="Main1" align="center" valign="middle">
		<table align="center" border="0" bgcolor="#DDDDDD" cellpadding="2" cellspacing="2">
		<tr>
		<td align="center" class="Main1"> You have successfully logged out. To return to Hammer and Hell click exit below. 
		</table>
		</form>
		<br>
		<table align="center" border="0" bgcolor="#DDDDDD" cellspacing="2" cellpadding="2">
		<tr>
		<td align="center" width="200" class="Main1"><a href="http://www.TestSite.com">Exit</a</td>
		</tr>
		</table>	
		</td>
	</tr>
</table>

?>
thanx much for any help!
soshea
User avatar
soshea
Forum Commoner
Posts: 31
Joined: Tue Nov 12, 2002 11:22 pm
Location: Idaho
Contact:

fixed the logout

Post by soshea »

whoops, i was referencing the old cookie i was testing with. the logout now works appropriately.
soshea
Post Reply