Basic PHP Login Script
Posted: Mon Nov 01, 2010 10:46 am
Thanks in advance to the experts who field this inquiry.
I am a budding web designer and am fairly versed with HTML and CSS. I have basic programming skills through courses in C and VB. I have been doing web design now for about a year and I now have the need to password protect a "members" page for a site I am building and I would like to use PHP to do this. I have the PHP basics as far as syntax, very basics, and how to assign variables and do basic loops which I found on W3Schools, the language is all very similar to C which I like. I have Googled my butt off as well as watching tutorials and I cannot make this thing work right. So now I am turning to the gurus for direction.
What I want:
I want a single page of the 8 page site I'm building to have a single login account simply to hide the donation page from folks who do not have the login. I have created the form within the page and wrote the basic IF/ELSE loop to lead to the members.php page.
What is happening:
For some reason the PHP is being ignored and when I click the "Submit" button the form functions correctly and displays the members.php page but without verifying the user/password from the form, it is going to the members.php page no matter what is typed.
My Code:
At the very beginning of the form.php page, the page containing my form, I have my variables called, this is before any other text(I believe this is correct placement):
<?php
$user = "admin";
$password = "password";
?>
Here is my form and IF/ELSE loop exactly how they appear in my page:
<form method="post" action="members.php">
<table width="200" border="0" cellpadding="3" cellspacing="5">
<tr>
<td>Username</td>
<td><input name="userForm" type="text" size="20" maxlength="20" /></td>
</tr>
<tr>
<td>Password</td>
<td><input name="passwordForm" type="password" size="20" maxlength="20" /></td>
</tr>
</table>
<center>
<input class="btn" type="submit" value="Login" />
</center>
</form>
<p>
<?php
if ($userForm == $user && $passwordForm == $password){
echo "Thank You!";
echo "Log-in Successful.";
}else{
echo "I'm sorry those are incorrect Log-in Credentials.";
echo "Please try again or contact a site administrator for the correct credentials.";
}
?>
</p>
Now eventually I want no echo if it is submitted successfully I simply want it to redirect to the members.php page with no message, not sure how this is accomplished yet guidance appreciated, I put in the echo's simply for testing.
FYI: I get the same results whether I test locally with Dreamweaver or post my files to the hosting server.
I just know this is a simple fix but as I do not have a PHP guru to ask and I'm moving in circles with the info I am finding online I am reaching out to you folks for some help.
Note: I also looked up the javascript solution for my task and I am not a fan of naming files the with the password etc, I would rather accomplish this with PHP.
Any help is appreciated!
Thanks for your time!!
-WS-
I am a budding web designer and am fairly versed with HTML and CSS. I have basic programming skills through courses in C and VB. I have been doing web design now for about a year and I now have the need to password protect a "members" page for a site I am building and I would like to use PHP to do this. I have the PHP basics as far as syntax, very basics, and how to assign variables and do basic loops which I found on W3Schools, the language is all very similar to C which I like. I have Googled my butt off as well as watching tutorials and I cannot make this thing work right. So now I am turning to the gurus for direction.
What I want:
I want a single page of the 8 page site I'm building to have a single login account simply to hide the donation page from folks who do not have the login. I have created the form within the page and wrote the basic IF/ELSE loop to lead to the members.php page.
What is happening:
For some reason the PHP is being ignored and when I click the "Submit" button the form functions correctly and displays the members.php page but without verifying the user/password from the form, it is going to the members.php page no matter what is typed.
My Code:
At the very beginning of the form.php page, the page containing my form, I have my variables called, this is before any other text(I believe this is correct placement):
<?php
$user = "admin";
$password = "password";
?>
Here is my form and IF/ELSE loop exactly how they appear in my page:
<form method="post" action="members.php">
<table width="200" border="0" cellpadding="3" cellspacing="5">
<tr>
<td>Username</td>
<td><input name="userForm" type="text" size="20" maxlength="20" /></td>
</tr>
<tr>
<td>Password</td>
<td><input name="passwordForm" type="password" size="20" maxlength="20" /></td>
</tr>
</table>
<center>
<input class="btn" type="submit" value="Login" />
</center>
</form>
<p>
<?php
if ($userForm == $user && $passwordForm == $password){
echo "Thank You!";
echo "Log-in Successful.";
}else{
echo "I'm sorry those are incorrect Log-in Credentials.";
echo "Please try again or contact a site administrator for the correct credentials.";
}
?>
</p>
Now eventually I want no echo if it is submitted successfully I simply want it to redirect to the members.php page with no message, not sure how this is accomplished yet guidance appreciated, I put in the echo's simply for testing.
FYI: I get the same results whether I test locally with Dreamweaver or post my files to the hosting server.
I just know this is a simple fix but as I do not have a PHP guru to ask and I'm moving in circles with the info I am finding online I am reaching out to you folks for some help.
Note: I also looked up the javascript solution for my task and I am not a fan of naming files the with the password etc, I would rather accomplish this with PHP.
Any help is appreciated!
Thanks for your time!!
-WS-