proble using array for user login

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
magosla2001
Forum Newbie
Posts: 20
Joined: Sat May 20, 2006 8:54 am

proble using array for user login

Post by magosla2001 »

I stored username and password of vistors i wanted to view some pages in my website in an associative array but I having problem getting it work that is to check if the value inputed in the login form matches an element in the array so as to grant the user access to the site but i am not able to get the exact script to do that and i need help for this below is the script;

Code: Select all

<?
session_start();

if (isset($_POST['execute'])) {
$un= $_POST['username'];
$pc= $_POST['passcode'];

$users;
$users["siasa"] = "123456";
$users["derack"] = "123457";
$users["joan"] = "123458";
$users["synthia"] = "123459";
$users["raymond"] = "123450";
$users["frtyu"] = "223456";
$users["ereres"] = "223457";
$users["wrdse"] = "223458";
$users["synthy"] = "323459";
$users["desmond"] = "323450";
$users["sopsa"] = "313456";
$users["derick"] = "823457";
$users["jane"] = "8923458";
$users["luopia"] = "12ok3459";
$users["randy"] = "1lk23450";
foreach( $users as $username => $password){
if (($un == $username) && ($pc == $password)) {
$_SESSION['userinfo'] = $_POST['username'];
header("Location: access.php");
}
 else {
header("Location: error.php");
     }
 }
}

else {
header("Location: login.html");
}
?>
Last edited by magosla2001 on Sun Sep 03, 2006 3:11 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

array_key_exists() may be of interest.
magosla2001
Forum Newbie
Posts: 20
Joined: Sat May 20, 2006 8:54 am

Post by magosla2001 »

How do I go about using it in the script to make it work
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Remove the foreach loop. In an if, use the function and check the value of the element when it exists. If the element's value matches the input, it was correct.
magosla2001
Forum Newbie
Posts: 20
Joined: Sat May 20, 2006 8:54 am

Post by magosla2001 »

Can somebody help me on a script to make it work that is to check if the datas inputed from the form i.e the username and the password field matches an element in the array because i am finding difficulties using array_key_exist()
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I told you how to do it. Instead of hoping that someone will code it for you, why not post what you tried to build that is detailed by the solution I provided?
magosla2001
Forum Newbie
Posts: 20
Joined: Sat May 20, 2006 8:54 am

Post by magosla2001 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


below is what I tried;

Code: Select all

<?
session_start();

if (isset($_POST['execute'])) {
$ac= $_POST['accesscode'];
$un= $_POST['username'];
$pc= $_POST['passcode'];

$users;
$users["lizzy"] = "123456";
$users["jack"] = "123457";
$users["raymond"] = "123458";
$users["rachel"] = "123459";
$users["tracy"] = "123450";
if (array_key_exists('$un', $users)) {
$_SESSION['userinfo'] = $_POST['username'];
header("Location: success.php");
}
 else {
 echo("why");
     }
 }


else {
echo "Error";
}
?>

I was unable to use it to check if the password field esist in the key value of the array element though is didn't work


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Last edited by magosla2001 on Mon Sep 04, 2006 12:00 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Single quotes are string literals. You're requesting the function to search for the literal "$un" not the contents of the variable.

After correcting that you'll need to check that the value of that element matches the password submitted before finally redirecting.

As a side note, make sure to use full URIs/URLs in header() based redirections. Web servers and web browsers that only implement the HTTP standards strictly will be unable to use relative redirections.
magosla2001
Forum Newbie
Posts: 20
Joined: Sat May 20, 2006 8:54 am

Post by magosla2001 »

I belive I am lost in this because this script is unable to check if the username field exist in as a key in the array. And I also dont know how to go about checking if the key value matches the password field. I tried checking if the username field matched the an array key but it doesn't work this was the little changes i made to the script;

Code: Select all

<?
session_start();

if (isset($_POST['execute'])) {
$ac= $_POST['accesscode'];
$un= $_POST['username'];
$pc= $_POST['passcode'];

$users;
$users["lisa"] = "123456";
$users["jack"] = "123457";
$users["ryan"] = "123458";
$users["rachel"] = "123459";
$users["grace"] = "123450";
if (array_key_exists("$un", $users)) {
$_SESSION['userinfo'] = $_POST['username'];
header("Location: http://127.0.0.1/text/success.php");
}
 else {
 echo("why");
     }
 }


else {
echo "Error";
}
?>
Post Reply