| View previous topic :: View next topic |
| Author |
Message |
jmbuford
Joined: 14 Apr 2007 Posts: 20
|
Posted: Tue Apr 24, 2007 12:35 pm Login Page - Password Protect |
|
|
|
Okay, I have a ton of pages that I want to password protect. I want them all to have the same password. I also want it so that I can edit the password on all of them by editing one. I don't want to use a database to do this, just HTML or PHP.
I used one and it worked except I couldn't edit all of the passwords at once and I tried to do a PHP include and that messed it up.
Thanks |
|
jmbuford
Joined: 14 Apr 2007 Posts: 20
|
Posted: Tue Apr 24, 2007 12:52 pm |
|
|
|
| I also want one where you can't look at the HTML by viewing the source and seeing where the end up page will be because that is easy to get by. |
|
SteveH
Joined: 11 Apr 2007 Posts: 208
|
|
jmbuford
Joined: 14 Apr 2007 Posts: 20
|
Posted: Tue Apr 24, 2007 1:15 pm |
|
|
|
| I looked at that. I am not able to use .htaccess. I was using PHP, but I want to be able to change the passwords to all of the pages all at once and I haven't found a code that will do that. Any other ideas? |
|
jmbuford
Joined: 14 Apr 2007 Posts: 20
|
Posted: Tue Apr 24, 2007 1:17 pm |
|
|
|
| I tried to do a PHP include on the password part, but wouldn't work because apparently you can't do a PHP include inside a PHP code. |
|
Corey Bryant Site Admin

Joined: 15 May 2004 Posts: 8301 Location: Castle Pines North, CO USA
|
Posted: Tue Apr 24, 2007 1:35 pm |
|
|
|
You can place PHP inside PHP code file includes. Of course, it might conflict with another piece of PHP code or you might need to check the paths.
You should have htaccess as well on your hosting provider. if not, you should see about moving to another company.
_________________
Corey
Toll Free Fax Numbers | Yahoo Merchant Account |
|
SteveH
Joined: 11 Apr 2007 Posts: 208
|
Posted: Tue Apr 24, 2007 1:37 pm |
|
|
|
| jmbuford wrote: |
| I tried to do a PHP include on the password part, but wouldn't work because apparently you can't do a PHP include inside a PHP code. |
That may indicate a code structure problem caused by the inclusion.
Post your code. Let me see your include file and a sample page that isn't working. |
|
jmbuford
Joined: 14 Apr 2007 Posts: 20
|
Posted: Tue Apr 24, 2007 2:00 pm |
|
|
|
| Code: |
<?php
if (isset($_COOKIE['loguser'])) {
$givenusername = $_COOKIE['loguser'];
} else {
$givenusername = $_POST['txtUsername'];
}
if (isset($_COOKIE['logpass'])) {
$givenpassword = $_COOKIE['logpass'];
} else {
$givenpassword = $_POST['txtPassword'];
}
switch ($givenusername){
<?php include("passwords.php"); ?>
break;
default:
$password = "no";
$givenpassword = "yes";
break;
}
if ($givenpassword != $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 {
setcookie ("loguser", $givenusername);
setcookie ("logpass", $givenpassword);
?>
<p>Content of Page Goes Herep>
<?php
}
?> |
Where is says:
| Code: |
| <?php include("passwords.php"); ?> |
That is where this should be located located:
| Code: |
case "user1":
$password = "pass1";
break;
case "user2":
$password = "pass2";
break;
case "admin":
$password = "adminpass"; |
Thanks
Last edited by jmbuford on Tue Apr 24, 2007 2:07 pm; edited 1 time in total |
|
SteveH
Joined: 11 Apr 2007 Posts: 208
|
Posted: Tue Apr 24, 2007 2:06 pm |
|
|
|
I'll look at this more closely, but try taking the <?php ?> wrapper off your include and see if that was the only problem.
From this:
| Code: |
| <?php include("passwords.php"); ?> |
To this:
| Code: |
| include("passwords.php"); |
|
|
jmbuford
Joined: 14 Apr 2007 Posts: 20
|
Posted: Tue Apr 24, 2007 2:10 pm |
|
|
|
| Thank you so much, it worked. |
|
SteveH
Joined: 11 Apr 2007 Posts: 208
|
Posted: Tue Apr 24, 2007 2:13 pm |
|
|
|
| Beauty. |
|
jmbuford
Joined: 14 Apr 2007 Posts: 20
|
Posted: Tue Apr 24, 2007 2:24 pm |
|
|
|
Okay, well maybe it didn't. I copied the original that had it all in one file. So it doesn't work.
Now this is the code that I have in login.php
| Code: |
<?php
if (isset($_COOKIE['loguser'])) {
$givenusername = $_COOKIE['loguser'];
} else {
$givenusername = $_POST['txtUsername'];
}
if (isset($_COOKIE['logpass'])) {
$givenpassword = $_COOKIE['logpass'];
} else {
$givenpassword = $_POST['txtPassword'];
}
switch ($givenusername){
include("passwords.php");
break;
default:
$password = "no";
$givenpassword = "yes";
break;
}
if ($givenpassword != $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 {
setcookie ("loguser", $givenusername);
setcookie ("logpass", $givenpassword);
?>
<p>This is the protected page. Your private content goes here.</p>
<?php
}
?> |
And in passwords.php
| Code: |
case "user1":
$password = "pass1";
break;
case "user2":
$password = "pass2";
break;
case "admin":
$password = "adminpass"; |
Sorry to get us excited that it worked first try. |
|
SteveH
Joined: 11 Apr 2007 Posts: 208
|
Posted: Tue Apr 24, 2007 3:04 pm |
|
|
|
| Hehe. That's just the way it is -- one step at a ta time. Give me a few to evaluate the script. |
|
SteveH
Joined: 11 Apr 2007 Posts: 208
|
Posted: Tue Apr 24, 2007 3:23 pm |
|
|
|
login.php:
| Code: |
<?php
if (isset($_COOKIE['loguser'])) {
$givenusername = $_COOKIE['loguser'];
} else {
$givenusername = $_POST['txtUsername'];
}
if (isset($_COOKIE['logpass'])) {
$givenpassword = $_COOKIE['logpass'];
} else {
$givenpassword = $_POST['txtPassword'];
}
include 'passwords.php';
if ($givenpassword != $password) {
?> |
passwords.php:
| Code: |
<?php
switch ($givenusername){
case ('user1'):
$password = 'pass1';
break;
case ('user2'):
$password = 'pass2';
break;
case ('admin'):
$password = 'adminpass';
break;
default:
$password = 'no';
$givenpassword = 'yes';
break;
}
?> |
|
|
SteveH
Joined: 11 Apr 2007 Posts: 208
|
Posted: Tue Apr 24, 2007 3:28 pm |
|
|
|
I think the issue has to do with the way PHP parses includes. When you include a file, even if it's all PHP, PHP will first begin evaluating the included file as HTML, so must encounter a <?php in the include file to switch to evaluating the file as PHP.
I believe I have that correct.
PHP switches again to HTML at the end of the document.
So the login was being parsed as PHP up until the switch statement, then switched to HTML for the case statements and choked. |
|
|