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>
Ilia Ross
  • 13,086
  • 11
  • 53
  • 88
Link
  • 303
  • 1
  • 5
  • 13
  • What error do you get? Also, why are you comparing the variables as strings? Why not just compare them directly? – David Sep 07 '12 at 17:31
  • 1
    The shouldn't be inside a if() clause... – Bgi Sep 07 '12 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. – Marc B Sep 07 '12 at 17:43

2 Answers2

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.

josh
  • 9,656
  • 4
  • 34
  • 51
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) {

Ilia Ross
  • 13,086
  • 11
  • 53
  • 88