HTML Tutorial


 Forum HomeForum Home   FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
RegisterRegister - Not registered yet? Got something to say? Join HTML Code Tutorial!
Login Page - Password Protect
Goto page 1, 2  Next
Post new topic   Reply to topic    HTML Help Forum Index -> PHP
View previous topic :: View next topic  
Author Message
jmbuford



Joined: 14 Apr 2007
Posts: 20

PostPosted: Tue Apr 24, 2007 12:35 pm     Login Page - Password Protect Reply with quote

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

PostPosted: Tue Apr 24, 2007 12:52 pm     Reply with quote

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

PostPosted: Tue Apr 24, 2007 1:06 pm     Reply with quote

Here's an option: .htaccess Password Protection
jmbuford



Joined: 14 Apr 2007
Posts: 20

PostPosted: Tue Apr 24, 2007 1:15 pm     Reply with quote

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

PostPosted: Tue Apr 24, 2007 1:17 pm     Reply with quote

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

PostPosted: Tue Apr 24, 2007 1:35 pm     Reply with quote



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

PostPosted: Tue Apr 24, 2007 1:37 pm     Reply with quote

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

PostPosted: Tue Apr 24, 2007 2:00 pm     Reply with quote

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

PostPosted: Tue Apr 24, 2007 2:06 pm     Reply with quote

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

PostPosted: Tue Apr 24, 2007 2:10 pm     Reply with quote

Thank you so much, it worked.
SteveH



Joined: 11 Apr 2007
Posts: 208

PostPosted: Tue Apr 24, 2007 2:13 pm     Reply with quote

Beauty.
jmbuford



Joined: 14 Apr 2007
Posts: 20

PostPosted: Tue Apr 24, 2007 2:24 pm     Reply with quote

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

PostPosted: Tue Apr 24, 2007 3:04 pm     Reply with quote

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

PostPosted: Tue Apr 24, 2007 3:23 pm     Reply with quote

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

PostPosted: Tue Apr 24, 2007 3:28 pm     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    HTML Help Forum Index -> PHP All times are GMT - 8 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
HTML Help Archive
Powered by phpBB © 2001, 2005 phpBB Group
HTML Help topic RSS feed 

 
HOSTING / DESIGN
MAKE MONEY

Home
  |   Tutorials   |   Forum   |   Quick List   |   Link Directory   |   About
Copyright ©1997-2002 Idocs and ©2002-2007 HTML Code Tutorial