473,403 Members | 2,183 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

ok... password protecting just one page

djsjm
23
Hello again. So I googled myself into finding this code:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Define your username and password
  3. $username = "someuser";
  4. $password = "somepassword";
  5.  
  6. if ($_POST['txtUsername'] != $username || $_POST['txtPassword'] != $password) {
  7. ?>
  8.  
  9. <h1>Login</h1>
  10.  
  11. <form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  12.     <p><label for="txtUsername">Username:</label>
  13.     <br /><input type="text" title="Enter your Username" name="txtUsername" /></p>
  14.  
  15.     <p><label for="txtpassword">Password:</label>
  16.     <br /><input type="password" title="Enter your password" name="txtPassword" /></p>
  17.  
  18.     <p><input type="submit" name="Submit" value="Login" /></p>
  19.  
  20. </form>
  21.  
  22. <?php
  23. }
  24. else {
  25. ?>
  26.  
  27. // put password protected content here
  28.  
  29. <?php
  30. }
  31. ?>
  32.  
Now, I thought I was pretty clever, until I tried it. It gives me a nice little password form, but it also displays the page content right along with it. And then, just for kicks and giggles, when I went ahead and entered the username and password I assigned, it took all of the supposedly protected content away and breaks my page apart. Kind of backwards, no?

Does it matter that there is php in the protected content?

<sigh>
Nov 4 '08 #1
8 1878
GazMathias
228 Expert 128KB
Odd, seems to work for me, though it complained about an undefined index, but other than that works as intended.
Nov 4 '08 #2
chelvan
90
hi
change your if condition. that means assign your posted values to the variable.

like this
Expand|Select|Wrap|Line Numbers
  1. if($variablename!=$_Post[variablename]){
  2. //login form
  3. }
  4. else{
  5. //password protected content
  6.  
  7. }
  8.  
  9.  
and remove 25th and 29th lines.

hope it will help you.

cheers
chel-1
Nov 4 '08 #3
Markus
6,050 Expert 4TB
hi
change your if condition. that means assign your posted values to the variable.

like this
Expand|Select|Wrap|Line Numbers
  1. if($variablename!=$_Post[variablename]){
  2. //login form
  3. }
  4. else{
  5. //password protected content
  6.  
  7. }
  8.  
  9.  
and remove 25th and 29th lines.

hope it will help you.

cheers
chel-1
Che, a couple of things:
  1. That code you supplied will give no different output to the original code (minus the errors below)
  2. $_POST has to be uppercase
  3. array indexes should be in quotes $_POST['index']

Djsjm, the code works fine for me. The only thing I can see a problem with is that you don't check to see if the form was submitted - you'll end up with an array index problem.
Nov 4 '08 #4
djsjm
23
hi there! ok... i changed it to this, based on everyone's advice:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Define your username and password
  3. $username = "xxxxxx";
  4. $password = "xxxxxx";
  5.  
  6. if($username!=$_POST['username'] || $password!=$_POST['password']){ 
  7. //login form 
  8.  
  9. <form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  10.     <p><label for="username">Username:</label>
  11.     <br /><input type="text" title="Enter your Username" name="username" /></p>
  12.  
  13.     <p><label for="password">Password:</label>
  14.     <br /><input type="password" title="Enter your password" name="password" /></p>
  15.  
  16.     <p><input type="submit" name="Submit" value="Login" /></p>
  17.  
  18. </form>
  19.  
  20. }
  21. else{ 
  22. //password protected content 
  23. ?>
  24.  
and got this error message...

Parse error: syntax error, unexpected '<' (took my address out for now) on line 51

my line 51 is line 9 above.

???
Nov 5 '08 #5
Markus
6,050 Expert 4TB
You can't output html in php. You either need to close the php block, or change the html into an echo/print statement.
Nov 5 '08 #6
djsjm
23
aha!! that's what i thought!!! but i was just following directions. :/

anyway, i echo'd it and it is still giving me the same error.
Nov 5 '08 #7
djsjm
23
and one more thing.... it REALLY hates that "else"... catches it every time and complains about it.

seems like it needs to be there... and i'm doing it exactly like i've been told... so i have no idea what i'm doing wrong.
Nov 5 '08 #8
Atli
5,058 Expert 4TB
How is you code looking now, and what exactly does it say when it complains about the "else"?

You need to properly separate the PHP and the HTML.
It needs to be either:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if($something == true) {
  3. ?>
  4. <div>If output</div>
  5. <?php
  6. } else {
  7. ?>
  8. <div>Else output</div>
  9. <?php
  10. }
  11. ?>
  12.  
Or:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if($something == true) {
  3.   echo "<div>If output</div>";
  4. }
  5. else {
  6.   echo "<div>Else output</div>";
  7. }
  8. ?>
  9.  
There is a much greater change of errors in the first example, so I would recommend something more like the second one.
Nov 5 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: edd | last post by:
Ive seen a few examples of how to password protect my pages by either textfiles / access. But how can I make the pages so that no one can just enter the url and bypass the login page. Im trying...
21
by: solomon_13000 | last post by:
I am using ms access database and asp 3.0 as my front end. In my database there is a table called account and a field called password. How do I protect the password stored in the database.
3
by: Miro | last post by:
Why Password protect an MDB when someone can google and get a hack? Wondering if anyone else has thought of this and just said "oh well"... I plan to password protect an MDB where I have some...
12
by: =?Utf-8?B?am9uaWdy?= | last post by:
I wrote a simple VB.NET application that imports and edits CSV files. Now I’d like to “lock” the raw (pre-import) CSV files so these cannot be opened separately. It is not high-sensitive...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.