Oddness with Login Script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
SilverMist
Forum Commoner
Posts: 65
Joined: Tue Mar 02, 2004 2:06 pm
Location: Canada
Contact:

Oddness with Login Script

Post by SilverMist »

Hi guys! I'm working on a login script where only me and another administrator can login. (As in it's not a login/register script.) It was all going wonderfully until I went to run it. If you put in the right password, you get to a page that says "Names names names!". If you put in the wrong password, you get something like "Wrong username/password". Or at least your supposed to. As of right now, you get the same page for right or wrong. I got the script when I was browsing through oooold topics, it's off something like page 714. Here's my code:

login.php

Code: Select all

<?php
function index()
{
echo "Login<br>
";
echo "<form action='?mode=login' method='post'>
Username: <input type='text' name='username' size='20'><br>
Password: <input type='password' name='password' size='20'><br>
<input type='submit' value='Log In'>
</form>
";
echo "";
}

$choice=$_GET['mode'];

switch($choice)
{
case "login":

$name = "Kate"; 
$name2 = "Abby"; 

$pass = "misty"; 
$pass2 = "tipsy"; 

if ( ($name != $name2) || ($pass != $pass2) ) { print "Names names names!\n";}

  else { print "Wrong username/password!!\n"; } 
break;


default:
index();
}

?>
Thanks in advance guys!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Slightly confused.

Your code just compare the variables $name and $name2; $pass and $pass2 and that's it.... different outputs but because your if() statement is satisfied based an the values given above your code will always output Names, names, names!....

Is it supposed to check that username and password are correct for the appropriate user?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Oddness with Login Script

Post by Chris Corbyn »

Code: Select all

<?php
function index()
{
echo "Login<br>
";
echo "<form action='?mode=login' method='post'>
Username: <input type='text' name='username' size='20'><br>
Password: <input type='password' name='password' size='20'><br>
<input type='submit' value='Log In'>
</form>
";
echo "";
}

$choice=$_GET['mode'];

switch($choice)
{
case "login":

$name = "Kate"; 
$name2 = "Abby"; 

$pass = "misty"; 
$pass2 = "tipsy";

$logins = array();
$logins[$name] = $pass;
$logins[$name2] = $pass2;

if ( isset($logins[$_POST['username']]) && $logins[$_POST['username']] == $_POST['password'] ) { print "Names names names!\n";}

  else { print "Wrong username/password!!\n"; } 
break;


default:
index();
}

?>
Post Reply