Page 1 of 1

proble using array for user login

Posted: Sun Sep 03, 2006 2:39 pm
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");
}
?>

Posted: Sun Sep 03, 2006 2:45 pm
by feyd
array_key_exists() may be of interest.

Posted: Sun Sep 03, 2006 2:56 pm
by magosla2001
How do I go about using it in the script to make it work

Posted: Sun Sep 03, 2006 3:01 pm
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.

Posted: Mon Sep 04, 2006 5:58 am
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()

Posted: Mon Sep 04, 2006 8:34 am
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?

Posted: Mon Sep 04, 2006 11:55 am
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]

Posted: Mon Sep 04, 2006 12:00 pm
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.

Posted: Mon Sep 04, 2006 12:44 pm
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";
}
?>