Basic PHP Login Script
Moderator: General Moderators
-
Whitesphynx
- Forum Newbie
- Posts: 8
- Joined: Mon Nov 01, 2010 10:10 am
Basic PHP Login Script
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-
Re: Basic PHP Login Script
hi .. if you are using DreamWeaver just create a form and then go to server behaviors click (+) > user authentication > Login user.
-
Whitesphynx
- Forum Newbie
- Posts: 8
- Joined: Mon Nov 01, 2010 10:10 am
Re: Basic PHP Login Script
Yes but then I need to create a database. I don't really want a database or an array.hi .. if you are using DreamWeaver just create a form and then go to server behaviors click (+) > user authentication > Login user.
I only have a single generic user/password to create that will probably only change once a year so having to deal with a database is way overkill for this application.
I have to be close to a solution using PHP, just need that extra kick to finish this off.
Thanks
-
internet-solution
- Forum Contributor
- Posts: 220
- Joined: Thu May 27, 2010 6:27 am
- Location: UK
Re: Basic PHP Login Script
The browser will load the members.php once submit button is pressed on your form. This is the expected behaviour. So, you have to move the authentication code to members.php.
You will also have to use $_POST variables in members.php to catch the values entered in the form.
You will also have to use $_POST variables in members.php to catch the values entered in the form.
Code: Select all
<?php
$user = "admin";
$password = "password";
$userForm=$_POST['userForm'];
$passwordForm=$_POST['passwordForm'];
if ($userForm == $user && $passwordForm == $password){
// display content of members.php
}else{
echo "I'm sorry those are incorrect Log-in Credentials.";
echo "Please try again or contact a site administrator for the correct credentials.";
}
?>-
Whitesphynx
- Forum Newbie
- Posts: 8
- Joined: Mon Nov 01, 2010 10:10 am
Re: Basic PHP Login Script
OK I tried that and its still doing the same thing.
I moved all the code to the members.php page.
All 4 variable calls are at the top of the members.php file exactly like you wrote with the $_POST call, as well as the loop and still it is loading the members.php page no matter what is entered.
I also tried moving the variable calls to the login.php page and only the loop in the members.php page and still just loads the darn page.
I really appreciate the help and hope to make this thing work right soon.
I moved all the code to the members.php page.
All 4 variable calls are at the top of the members.php file exactly like you wrote with the $_POST call, as well as the loop and still it is loading the members.php page no matter what is entered.
I also tried moving the variable calls to the login.php page and only the loop in the members.php page and still just loads the darn page.
I really appreciate the help and hope to make this thing work right soon.
-
Whitesphynx
- Forum Newbie
- Posts: 8
- Joined: Mon Nov 01, 2010 10:10 am
Re: Basic PHP Login Script
I tried simplifying everything to get this to work and it seems the info just isn't being passed
I eliminated the IF loop and am just trying to display what is entered into the form on the members.php page with no avail.
So I did the above with no variable calls and put it into the body of the members.php page so the form should pass the 2 variable to this code. According to W3Schools that should display the contents of the form with matching variables but it does not. Just blank no matter what is entered into the form. 
I eliminated the IF loop and am just trying to display what is entered into the form on the members.php page with no avail.
Code: Select all
<p>
Username is <?php echo $_POST["userForm"]; ?><br />
Password is <?php echo $_POST["passwordForm"]; ?>
</p>Re: Basic PHP Login Script
index.php
members.php
failed.php
Code: Select all
<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" value="" /></td>
</tr>
<tr>
<td>Password</td>
<td><input name="passwordForm" type="password" size="20" maxlength="20" value="" /></td>
</tr>
</table>
<center>
<input class="btn" type="submit" value="Login" />
</center>
</form>members.php
Code: Select all
<?php
$user="admin";
$pass="password";
$userForm=$_POST['userForm'];
$passwordForm=$_POST['passwordForm'];
if ($userForm == $user && $passwordForm == $pass){?>
Welcome (CONTENT GOES HERE)
<?php
}else{
$failed="failed.php";
header ("Location: " . $failed);
}
?>Code: Select all
<html>
<head>
<title>Login failed</title>
</head>
<body>
LOGIN FAILED
</body>
</html>-
Whitesphynx
- Forum Newbie
- Posts: 8
- Joined: Mon Nov 01, 2010 10:10 am
Re: Basic PHP Login Script
That looks great! Thanks for all you help.
Still not working though.

Still not working though.
Last edited by Whitesphynx on Tue Nov 02, 2010 2:10 pm, edited 1 time in total.
-
Whitesphynx
- Forum Newbie
- Posts: 8
- Joined: Mon Nov 01, 2010 10:10 am
Re: Basic PHP Login Script
Might have found the issue...will update.
-
Whitesphynx
- Forum Newbie
- Posts: 8
- Joined: Mon Nov 01, 2010 10:10 am
Re: Basic PHP Login Script
OK I got past the form passing issue and I have input your code Amir. I also created the failed.php file. It works great on legit creds but on invalid creds it still takes me to the members.php page and displays this error on the very top of the screen:
Warning: Cannot modify header information - headers already sent by (output started at /mysite/members.php:2) in /mysite/members.php on line 15
Any ideas? If I can just get this redirect on invalid credentials I'll be golden.
Thanks again for all the info!
Warning: Cannot modify header information - headers already sent by (output started at /mysite/members.php:2) in /mysite/members.php on line 15
Any ideas? If I can just get this redirect on invalid credentials I'll be golden.
Thanks again for all the info!
Re: Basic PHP Login Script
mmm... but I test it on my localhost and it is not giving me any error
... try to put @ before header()...
Re: Basic PHP Login Script
Any whitespace before the <?php tag? New lines? <?php must be the very first thing on the page.Whitesphynx wrote:OK I got past the form passing issue and I have input your code Amir. I also created the failed.php file. It works great on legit creds but on invalid creds it still takes me to the members.php page and displays this error on the very top of the screen:
Warning: Cannot modify header information - headers already sent by (output started at /mysite/members.php:2) in /mysite/members.php on line 15
Any ideas? If I can just get this redirect on invalid credentials I'll be golden.
Thanks again for all the info!
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Re: Basic PHP Login Script
Maybe you can provide your members.phpWhitesphynx wrote:OK I got past the form passing issue and I have input your code Amir. I also created the failed.php file. It works great on legit creds but on invalid creds it still takes me to the members.php page and displays this error on the very top of the screen:
Warning: Cannot modify header information - headers already sent by (output started at /mysite/members.php:2) in /mysite/members.php on line 15
Any ideas? If I can just get this redirect on invalid credentials I'll be golden.
Thanks again for all the info!
header will not work if there's already an output. Like an echo (on line 2)..
-
Whitesphynx
- Forum Newbie
- Posts: 8
- Joined: Mon Nov 01, 2010 10:10 am
Re: Basic PHP Login Script
Ok great thanks guys!
Yes s.dot it was that exactly I had a blank line at the top the page before the <php started for ease of reading. I didn't know about that issue.
I knew it was something simple.
Thanks so much Amir and s.dot. I know this is some newby issues with PHP but now I have some better knowledge of the language should there come a time to work with a database.
And maybe if there's another PHP newb like me out there wanting to have the same basic login script they will find this thread.
Cheers!
-WS-
Yes s.dot it was that exactly I had a blank line at the top the page before the <php started for ease of reading. I didn't know about that issue.
I knew it was something simple.
Thanks so much Amir and s.dot. I know this is some newby issues with PHP but now I have some better knowledge of the language should there come a time to work with a database.
And maybe if there's another PHP newb like me out there wanting to have the same basic login script they will find this thread.
Cheers!
-WS-