Jump to content

[SOLVED] Having problems w/ a simple login scripts. (Where's Newbie Help?)


Recommended Posts

I'm very new to PHP.  In fact, I wasn't even sure what my search criteria would be.  I just started reading the Zend tutorials last week and now I'm trying to make simple programs on my own.  Everything has been great until I tried to program a log in script.  I thought it'd be simple enough.

 

The problem is, no matter what, it echos "Wrong!" even when the username and password are correct.  I've tried everything and nothing seems to work.  Also, I'm using PHP 4 on Apache 2.0.

 

HTML form

<html>
<head>
<title></title>
</head>
<body>

<h2>Please login</h2>
<br />

<form action="password.php" method="get">
Username: <input type="text" name="username" size="30"><br /><br />
Password: <input type="text" name="password" size="30"><br /><br />
<input type="submit" value="submit">
</form>

</body>
</html>

 

PHP script

<html>
<head>
<title></title>
</head>
<body>

<?php

$filehandle = 'UserandPass.txt' or die('Could not read file!');

$data = file($filehandle) or die('Could not read file!');

if ($_GET['username'] == $data[0] && $_GET['password'] == $data[1]) {
$message = "welcome";
}
else {
$message = "Wrong!";
}

echo $message;

?>

</body>
</html>

 

UserandPass.txt

brawley
christmas

Try to trim() all variables inside the if()

 

instead of:

if ($_GET['username'] == $data[0] && $_GET['password'] == $data[1]) {

 

change to

if (trim($_GET['username']) == trim($data[0]) && trim($_GET['password']) == trim($data[1])) {

Try to trim() all variables inside the if()

 

instead of:

if ($_GET['username'] == $data[0] && $_GET['password'] == $data[1]) {

 

change to

if (trim($_GET['username']) == trim($data[0]) && trim($_GET['password']) == trim($data[1])) {

 

Thanks a lot, man.  That worked.  I'm going to google trim().  Either it wasn't mentioned in the Zend tutorial or I haven't quite made it that far yet.  Either way, thanks again.

I'd also use method of POST rather than GET.

 

GET appends the form data onto the URL whereas POST sends a packet of data instead and can't be seen.

 

You're absolutely right.  I'll definitely change that.  Thanks for your input.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.