Forum Moderators: coopster

Message Too Old, No Replies

Need help with password protected page

         

smiddy

10:07 pm on Sep 14, 2004 (gmt 0)

10+ Year Member



I was able to get my photo gallery going. I then tried to limit access to this page from strangers by password protecting it. The script is as follows...

<?php
// Define your username and password
$username = "guest";
$password = "letmein";
if ($_POST['txtUsername']!= $username ¦¦ $_POST['txtPassword']!= $password) {
?>
<h1>Login</h1>
<form name="form" method="post" action="<?php echo $_SERVER
['PHP_SELF'];?>">
<p><label for="txtUsername">Username:</label>
<br /><input type="text" title="Enter your Username"
name="txtUsername" /></p>
<p><label for="txtpassword">Password:</label>
<br /><input type="password" title="Enter your password"
name="txtPassword" /></p>
<p><input type="submit" name="Submit" value="Login" /></p>
</form>
<?php
}
else {
?>
<p>
Gallery PHP script here that has an array function that does looping through photos. LONG stuff here.

</p>
<?php
}
?>

So the problem I have is that every time I click for the next photo in the array on the page, it asks me again for the password. Is there a way to password protect the page on the first entry only?

willybfriendly

11:12 pm on Sep 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sessions and/or cookies

WBF

smiddy

12:05 pm on Sep 15, 2004 (gmt 0)

10+ Year Member



huh?

coopster

4:12 pm on Sep 15, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The issue you are desribing is relative to the nature of the HTTP protocol. It is a "stateless" protocol, meaning the requester makes a request of a server, there is a connection to the server, the server processes the request, returns the information requested, and disconnects from the requester.

There are varying techniques of "maintaining state" including hidden form fields (<input type="hidden"...>), cookies, sessions, etc. Basically, certain *pieces* of information are left on the client-side which help the server recognize the request coming from a previous requester, and the server can use the *stored* information left over from the last request to do more processing.

There are a number of ways to incorporate password protection. One of the most common and most secure (overall) is Session Handling [php.net].