Forum Moderators: coopster
$acc = [url=http://www.php.net/eval]eval[/url]("\$login == \"bob\" && \$pass == \"3258\"");
Why would you do anything like this, though?
Im trying to make a very simple login & pass protected page. It's for a single page and can have multiple accounts. I want the user to be able to relatively easily modify the php code to add accounts (or variable login conditions). It would be nice if I could have just the stuff they edit at the very top.
for instance.
this happens later down in my code, and Id like the $acc to be what they can edit at the top.
if(isset($_POST['submit'])){
$login = $_POST['login'];
$pass = $_POST['pass'];
if($acc){
$go = "true";
} else {
$go = "false";
}
}
here is what I have at the top
$acc = eval($login == "bob" && $pass == "2439");
but this isnt working, also id like it to end up something like this
$acc = eval($login == "bob" && $pass == "2439" ¦¦
$login == "chuck" && $pass == "3345" ¦¦
$login == "dogg" && $pass == "1123"
);
if(isset($_POST['submit'])){
$login = $_POST['login'];
$pass = $_POST['pass'];
if ($login == 'bob' && $pass == '1234') {
//if($acc){
$go = "true";
} else {
$go = "false";
}
}
<edit>
Sorry just noticed the bit at the bottom of your last post. So you want to have more than 1 person able to log in.
$user = array('bob' => '1234', 'andy' => 'letMeIn', 'fred' => 'pasword');
if ($user["$login"] == $pass){
$go = 'true';
}
else {
$go = 'false';
}
[edited by: PHP_Chimp at 8:43 pm (utc) on Nov. 8, 2007]
I'm still trying to completely grasp arrays, in this example (or all) is $login identifing which row in the array?
$user["$login"]
and then from there getting the value of that row..?
I just need someone to explain to me how arrays work & what exactly is happening , the basics...
also some examples of how they are often used and usefull
THANKS
$user[...this is the key...]
So $user[$login] could be said to be something like -
Look in the $user array for the key $login (so bob or whoever).
Try -
echo '<pre>';
print_r($user);
echo '</pre>';
echo 'This is the value for ['bob']'.$user['bob'];
as this will show you how it works.
Quote from the manual [php.net]
A key may be either an integer or a string. If a key is the standard representation of an integer, it will be interpreted as such (i.e. "8" will be interpreted as 8, while "08" will be interpreted as "08"). Floats in key are truncated to integer. There are no different indexed and associative array types in PHP; there is only one array type, which can both contain integer and string indices.
Same thing I said from the manual ;) -
Array do's and don'ts
Why is $foo[bar] wrong?You should always use quotes around a string literal array index. For example, use $foo['bar'] and not $foo[bar]. But why is $foo[bar] wrong? You might have seen the following syntax in old scripts:
<?php
$foo[bar] = 'enemy';
echo $foo[bar];
// etc
?>
This is wrong, but it works. Then, why is it wrong? The reason is that this code has an undefined constant (bar) rather than a string ('bar' - notice the quotes), and PHP may in future define constants which, unfortunately for your code, have the same name. It works because PHP automatically converts a bare string (an unquoted string which does not correspond to any known symbol) into a string which contains the bare string. For instance, if there is no defined constant named bar, then PHP will substitute in the string 'bar' and use that.
[edited by: PHP_Chimp at 9:49 pm (utc) on Nov. 8, 2007]
here is what I have
$go = "";
if(isset($_POST['sub'])){
$login = $_POST['login'];
$pass = $_POST['pass'];
if((empty($_POST['login'])) ¦¦ (empty($_POST['pass']))){
$go = "false";
}
$user = array('bob' => '1234', 'andy' => 'letMeIn', 'fred' => 'pasword');
if($go!= "false"){
if ($user["$login"] == $pass){
$go = 'true';
} else {
$go = "false";
}
}
}
$go = null; // not really needed but if you want to initialize the variable.
if(isset($_POST['sub'])){
$login = $_POST['login'];
$pass = $_POST['pass'];
} // end this here as you are just passing variables.
if((empty($_POST['login']) ¦¦ (empty($_POST['pass'])){
// you dont need as many ( or )'s above
$go = false; // remove the quotes
}
$user = array('bob' => '1234', 'andy' => 'letMeIn', 'fred' =>'pasword');
if($go!== false && $user["$login"] == $pass){
$go = true;
}
else {
$go = false;
}
What error were you getting?
//if(isset($_POST['sub'])){
$login = 'bob';//$_POST['login'];
$pass = '1234';//$_POST['pass'];
//}
if (empty($login) ¦¦ empty($pass)){
$go = false;
}
$user = array('bob' => '1234', 'andy' => 'letMeIn', 'fred' =>'pasword');
if($user["$login"] == $pass){
$go = true;
}
else {
$go = false;
}
print $go? 'true': 'false';
?>
I have just tested this and it works.
If you are copying and pasting then make sure that you replace the ¦ with a verticle bar as the forum breaks them.
Also note that for the test i have had to comment out a load of stuff as there are no $_POST variables getting passed across.
<edit1>
You would be better off initializing $go as false, as that will keep people out. So when you get to if($user["$login"] == $pass){ you cant test to see if $go is not false, as it will always be false at that point, so no point in testing it here. Just check for a valid password.
<edit2>
You can get rid of the else part of the final loop, as $go is already set to false...so no need to reset it to false.
Its late, so im allowed to be an idiot ;)
<edit3>
You can also get rid of -
if (empty($login) ¦¦ empty($pass)){
$go = false;
}
as again its setting $go to false and it is already there...
Seeing as this is turning into the longest post in the world -
You may want to start with $go =0;
Then check for empty's and if it is empty then $go = 1;
Then check for valid password. if valid $go = 2; else $go = 3;
Then you could output error messages based on
1 = fill in the form
2 = you are allowed in
3 = have another go at your pasword
Im off to bed as I obviously need some sleep...
[edited by: PHP_Chimp at 10:16 pm (utc) on Nov. 8, 2007]
If I enter nothing and hit sub, I get "invalid login" GOOD
If I enter just valid or invalid name and no pass, I get "invalid login" GOOD
If I enter just valid or invalid pass and no name, I get "invalid login" GOOD
If I enter an invalid name and an invalid/valid pass I get this error
Notice: Undefined index: #*$!x in C:\Program Files\Apache Software Fo....
where #*$!x is the invalid name I entered
Here is my code, My server has all error reporting on (and Id like to keep it that way) Im guessing that I need to null or set that variable somehow, but not sure..?
$user = array('bob' => '1234', 'andy' => 'letMeIn', 'fred' =>'pasword');
$go = 2;
if(isset($_POST['sub'])){
$login = $_POST['login'];
$pass = $_POST['pass'];
if (empty($login) OR empty($pass)){
$go = 0;
} else {
if($user["$login"] == $pass){
$go = 1;
} else {
$go = 0;
}
}
}
$user = array('bob' => '1234', 'andy' => 'letMeIn', 'fred' =>'pasword');
$go = 2;
if(isset($_POST['sub'])){
$login = $_POST['login'];
$pass = $_POST['pass'];
if (empty($login) OR empty($pass)){
$go = 0;
} else {
if (array_key_exists("$login",$user)){
if($user["$login"] == $pass){
$go = 1;
} else {
$go = 0;
echo "key good, pass bad";
}// matches
} else {
$go = 0;
echo "key not found";
} //exists
} //empty
} //set