0

I'm trying to hide a content of a page using PHP but always get an error this is the code I'm using. Data from $pass comes from a form submited from the other page:

<?php
 $pass = $_POST['pass'];
 $password = "content";  // Modify Password to suit for access, Max 10 Char.
?>

<html>
<title></title>
<head>

<?php 
 // If password is valid let the user get access
 if ( "$pass" == "$password") {
?>

<!-- START OF HIDDEN HTML - PLACE YOUR CONTENT HERE -->
</head>
<body>
You have gained access!
<!-- END OF HIDDEN HTML -->

<?php 
 } else {
   // Wrong password or no password entered display this message
   print "<p align=\"center\"><font color=\"red\"><b>Restricted Area!</b><br>Please enter from the log in page</font></p>";}
}
?>

</body>
</html>
3
  • What error do you get? Also, why are you comparing the variables as strings? Why not just compare them directly? Commented Sep 7, 2012 at 17:31
  • 1
    The </head><body> shouldn't be inside a if() clause... Commented Sep 7, 2012 at 17:33
  • "$pass" and "$password" - the quotes are useless. $pass == $password will work the same. using quotes like that is evidence of cargo-cult programming. Commented Sep 7, 2012 at 17:43

2 Answers 2

1
print "<p align=\"center\"><font color=\"red\"><b>Restricted Area!</b><br>Please enter from the log in page</font></p>";}

On this line, you have an extra curly brace at the end. So in your file, you have an extra curly brace overall. Try changing this line to

print "<p align=\"center\"><font color=\"red\"><b>Restricted Area!</b><br>Please enter from the log in page</font></p>";

And it should work.

Sign up to request clarification or add additional context in comments.

Comments

0

Link, your code didn't run and ended up with a fatal error simply because you had one extra } after the print, meaning your else ended with one too many }. Remove it and then your code will run without any errors:

Online Working Example

In addition, if you compare variables, you must not use quotes ", try doing this:

if ( $pass == $password) {

2 Comments

it works thank you!!! just had a mistyped value on the form on the other page but thanks for the response!
I was happy to help you! Good luck to you, Link! ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.