Jump to content

Trouble with arrays


fgatlin

Recommended Posts

Trying to get my login script to echo the correct user name. Currently, it is only displaying the first username even when logged in as user 2

<?php {if ($login='user1'){$login2='User One';}else if ($login='user2'){$login2='User Two';}echo $login2;}?> This small script is being called via and include script.

Any help is much appreciated.

Edited by fgatlin
Link to comment
Share on other sites

What's feeding the script? That's where your question starts.

Edited by niche
  • Like 1
Link to comment
Share on other sites

You're using an assignment operator. ($login='user1') sets the value of $login to "user1" You should use the comparison operator, ==.

  • Like 1
Link to comment
Share on other sites

The login script is one of those you find on the Internet:

$LOGIN_INFORMATION = array(   'user1' => 'password',  'user2' => 'password',);// request login? true - show login and password boxes, false - password box onlydefine('USE_USERNAME', true);// User will be redirected to this page after logoutdefine('LOGOUT_URL', 'URL');// time out after NN minutes of inactivity. Set to 0 to not timeoutdefine('TIMEOUT_MINUTES', 1);// This parameter is only useful when TIMEOUT_MINUTES is not zero// true - timeout time from last activity, false - timeout time from logindefine('TIMEOUT_CHECK_ACTIVITY', true);###################################################################  SETTINGS END##################################################################///////////////////////////////////////////////////////// do not change code below///////////////////////////////////////////////////////// show usage exampleif(isset($_GET['help'])) {  die('Include following code into every page you would like to protect, at the very beginning (first line):<br><?php include("' . str_replace('\\','\\\\',__FILE__) . '"); ?>');}// timeout in seconds$timeout = (TIMEOUT_MINUTES == 0 ? 0 : time() + TIMEOUT_MINUTES * 60);// logout?if(isset($_GET['logout'])) {  setcookie("verify", '', $timeout, '/'); // clear password;  header('Location: ' . LOGOUT_URL);  exit();}if(!function_exists('showLoginPasswordProtect')) {// show login formfunction showLoginPasswordProtect($error_msg) {?>  <div style="width:500px; margin-left:auto; margin-right:auto; text-align:center">  <form method="post">    <h3>Please enter your Melco Username and Password again</h3>    <h4>(For added Security)</h4>    <font color="red"><?php echo $error_msg; ?></font><br /><?php if (USE_USERNAME) echo 'Login:<br /><input type="input" name="access_login" /><br />Password:<br />'; ?>    <input type="password" name="access_password" /><p></p><input type="submit" name="Submit" value="Submit" />  </form>  <br />  </div><?php  // stop at this point  die();}}// user provided passwordif (isset($_POST['access_password'])) {  $login = isset($_POST['access_login']) ? $_POST['access_login'] : '';  $pass = $_POST['access_password'];  if (!USE_USERNAME && !in_array($pass, $LOGIN_INFORMATION)  || (USE_USERNAME && ( !array_key_exists($login, $LOGIN_INFORMATION) || $LOGIN_INFORMATION[$login] != $pass ) )  ) {    showLoginPasswordProtect("Incorrect password.");  }  else {    // set cookie if password was validated    setcookie("verify", md5($login.'%'.$pass), $timeout, '/');       // Some programs (like Form1 Bilder) check $_POST array to see if parameters passed    // So need to clear password protector variables    unset($_POST['access_login']);    unset($_POST['access_password']);    unset($_POST['Submit']);  }}else {  // check if password cookie is set  if (!isset($_COOKIE['verify'])) {    showLoginPasswordProtect("");  }  // check if cookie is good  $found = false;  foreach($LOGIN_INFORMATION as $key=>$val) {    $lp = (USE_USERNAME ? $key : '') .'%'.$val;    if ($_COOKIE['verify'] == md5($lp)) {	  $found = true;	  // prolong timeout	  if (TIMEOUT_CHECK_ACTIVITY) {	    setcookie("verify", md5($lp), $timeout, '/');	  }	  break;    }  }  if (!$found) {    showLoginPasswordProtect("");  }}?>

And then, in the footer where I want the information to echo to:

<?php include 'addons/loginArray.php'; ?>(which is the first script posted)

Link to comment
Share on other sites

You're using an assignment operator. ($login='user1') sets the value of $login to "user1" You should use the comparison operator, ==.
Perfect! Thank you ever so much.
Link to comment
Share on other sites

Obviously, to this day, I still miss that. Too many decades of programming where that wasn't an issue. Please excuse me.

Edited by niche
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...