0

am creating a simple php login script and testing it in firefox before uploading the server but to my suprise it fails each time.
//index.php

<form name="form1" method="post" action="checklogin.php">
    username<input name="myusername" type="text" id="myusername">
    password<input name="mypassword" type="password" id="mypassword">
    <input type="submit" name="Submit" value="Login">
</form>


//checklogin.php

<?php
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword']; 
if($myusername==='stackoverflow')
{
    if($mypassword==='stackoverflow')
    {
        session_register("myusername");
        session_register("mypassword");
        header("location:login_success.php");
    }
}

else echo ("<h2>wrong username or password</h2><br><a href='index.php'>click here to try again</a>");
?>


when i drop the index.php script into firefox 4 and type username and password it always shows wrong username or password. I thought this script was basic but it still eludes me. Please help.

3
  • 2
    You are aware that your script will only output something if the wrong username is entered, but if it is the correct username with a wrong password it will print a blank page?
    – Bobby
    Jul 8, 2011 at 8:09
  • Its working in my browser ! Try removing cookies
    – Nitish
    Jul 8, 2011 at 8:13
  • 1
    Hey Denis, what exactly do you mean by "drop the index.php" file into firefox? You'll need to upload it to your local development server and open it as a URL in your Firefox. Just want to confirm if you are doing that.
    – Keshav
    Jul 8, 2011 at 8:17

4 Answers 4

1

You are using:

===

TRUE if $a is equal to $b, and they are of the same type. (introduced in PHP 4)

Try using:

==

$a == $b Equal TRUE if $a is equal to $b after type juggling.

See if that makes a difference.

0

add var_dump($_POST) to your script and compare the results with the expected values

0

Make sure you call:

session_start();

at the top of your script, may also help.

But as @Bobby said, you will only be redirected if your username and password are correct.. And in this case, there is no username and password yet.

0

Make sure you have typed header("location:login_success.php");

and not

header("location:checklogin.php");

in your editor !

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.