hello.. I am just testing a simple PHP password/verify form... and I don't know why its not working. I am new to PHP but I have looked at other sample codes and mine seems to be okay in my eyes...
I have two files... password.html and password.php.

password.html looks like this:

<title> Password Script </title>
<form name="form1" method="post" action="password.php">
<input name="username" type="text" id="username">
<p>Enter your First Name Please</p>
<input type="submit" name="Submit" value="Submit">

and password.php looks like this:

if ($username == "Rob")
print "Yay, Rob you are logged in!";
print "I am sorry the password is incorrect. Please go back and try again";

Now when I enter Rob as the name, i STILL get "I am sorry the password is incorrect. Please go back and try again" ... I don't understand why this is happening. No matter what I put as the password in the form, I still get the I am sorry message. Please help... this is probably something really simple but I'm just too new at this to catch it. The server is running PHP 4.3.4 and is hosted with OSX Panther Server 10.3.

I just want to be able to create small password frms, where someone can enter a password and proceed to the next page.
try it for yourself http://www.impassible.com/documents/learnphp/password.html

Thanks alot...

Recommended Answers

All 5 Replies

Well it's no doubt because the server administrator is sane.

Try changing $username to $_POST. By default PHP won't assign external variables passed through forms, cookies, and URLs for security reasons. It's pretty obvious why - otherwise, anyone who had seen the script (or who hadn't and was just guessing) could add something like &admin=1 or something and find themselves granted admin access. Additionally, it means that you can tell where the varibales came from - be a form ($_POST), a URL ($_GET), or a cookie ($_COOKIE).

Note, however, that if your form's method is set to get instead of post, that you'll need to retreive the variable through $_GET. But this will rarely, if ever, be an issue.

Thanks alot, that worked perfectly! As simple as that is it will help me out a lot. :)

can I ask one more thing?
I am reading the book "Teach yourself PHP in 24 hours"... is a pain in the ass for me because I am dyslexic but I am forcing myself to learn cause PHP is wonderful... I have gotten to hour 8, and one thing it never explained to me in a logical way (at least I never grasped it) was what "as" means/does. Its been used since the earlier hours of the book, but I have just been using it not really knowing exactly what it means. Can you give me a good explination so that from now on, when I use it, I'll know why?

foreach ($first as $key=>$val)

thanks in advance!

ok, have you covered arrays? I will assume you have for the moment. Otherwise, the following will only confuse you. :p

AFAIK, as is only used in a foreach() statement.

Let's say that you have an array of people's names and that you want to correctly capitalise each name (for the moment we'll exclude Scottish People who are McSomething for simplicty).

One way of doing it, would be as follows.

/* The names are defined below. I prefer to write arrays out like that to ensure they're easy to read, but the format is up to personal preference. Because you may have a different monitor size to me, I'm using /* instead of // to comment. (So you can see that all this is actually supposed to be one line) */

$people = array(

/* Next, we want to look at each item in the list and correct the capitialisation. */

foreach( $people as $person )
    /* What's happened here? Well, this is a special kind of loop that loops through each item in the array $people, and assigns the value each time to $person. So, the first time this loops, $person will be equal to 'JAsmine'. The next; 'cROwsteN', and so forth. */

    echo '<br />Person: ' . ucfirst(strtolower($person));

    /*That essentially turns the value of $person to completely lower case (strtolower) and then changes the first letter to upper case (ucfirst), the echos it following '<br />Person: '. */

Ok, now, suppose for some reason, you want to actually know which index that person had and echo that along with it? Well, you can do a similar thing like so:

$emails = array(
    'bob'    => 'bob@bobby.com',
    'rob'    => 'rob@rob.com',
    'me'     => 'me@you.com'

foreach( $emails as $name => $email )
   echo "{$name}'s email is $email. <br />";

Will echo something like:
bob's email is bob@bobby.com
rob's email is rob@rob.com

I hope that clears up any confusion.

Thanks! That explained it pretty well. Much better than the book. It all makes sense now. :)

Books can only take you so far, and they're often quite outdated - though I'm surprised that it's assuming request (sent in forms, urls, cookies) variables have been assigned to normal variables automatically. It's been default not to since PHP4, and the PHP people have been blathering on about it since early PHP3.

But anyway, just come back here if ya need any more help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.