Jump to content

Help with client login


lookinfreshuk

Recommended Posts

hello,

 

i want to create a client login system so when they use there login it redirects them to a page only for them, is this possible? i am using the following code but it only re-drirects them after login to the "index.php" page not the 2nd user to "Harts.php", what am i doing wrong?, thanks

 

<?php

$usernames = array("admin","admin2");

$passwords = array("admin","admin2");

$page = array("index.php","Harts.php");

 

for($i=0;$i<count($usernames);$i++)

{

  $logindata[$usernames[$i]]=$passwords[$i];

}

 

$found = 0;

for($i=0;$i<count($usernames);$i++)

{

  if ($usernames[$i] == $_POST["username"])

  {

  $found = 1;

  }

}

if ($found == 0)

{

  header('Location: login.php?value=fail');

  exit;

}

 

if($logindata[$_POST["username"]]==$_POST["password"])

{

  session_start();

  $_SESSION["username"]=$_POST["username"];

  header('Location: '.$page);

  exit;

}

else

{

  header('Location: login.php?value=fail');

  exit;

}

?>

 

Link to comment
https://forums.phpfreaks.com/topic/214391-help-with-client-login/
Share on other sites

Hey LookinfreshUK, try the following in place of your current code.

 

<?PHP
  $usernames = array('admin','admin2');
  $passwords = array('admin','admin2');
  $page      = array('index.php','Harts.php');
  
  // Count the amount of users in array.
  $count_users = count($usernames);

  // Set users found to "0".
  $user_found  = '0';

  // Set username and password variables from $_POST data.
  $username = $_POST['username'];
  $password = $_POST['password'];

  // Check to see if we have a match in usernames
  // If we have a match set a variable with the record number
  // Update "$user_found" if we find a match
  for($u=0; $u<$count_users; $u++) {
    if($username == $usernames[$u]) { 
      $userRecord = $u; 
      $user_found = '1';
    }
  }

  // If no user is found, login has failed. Re-direct back to login.
  if(!$user_found) { 
   header('Location: login.php?value=fail');
   exit;
  }

  // If usernames don't match, login has failed. Re-direct back to login.
  if($usernames[$userRecord] != $username) {
   header('Location: login.php?value=fail');
   exit;

  // If passwords don't match, login has failed. Re-direct back to login.
  } else if($passwords[$userRecord] != $password) {
   header('Location: login.php?value=fail');
   exit;

  // If usernames and passwords match, login succeeded. Re-directed to player specific page.
  } else {
   $_SESSION['username'] = $username;
   header("Location: $page[$userRecord]");
   exit;
  }
?>

 

Tell me if it works or not bud :)

 

Regards, Paul.

NEW CODE AS YOU PUT ^^

 

<?PHP
$usernames = array('admin','harts');
$passwords = array('admin','harts');
$page = array('index.php','Harts.php');

// Count the amount of users in array.
$count_users = count ($usernames);

// Set users found to "0"
$user_found = '0';

// Set username and password variables from $_POST data.
$username = $_POST['username'];
$password = $_POST['password'];

// Check to see if we gave a match in usernames
// If we have a match set a cariable with the record number
// Update "$user_found" if we find a match
for($u=0; $u<$count_users; $u++) {
if($username == $usernames[$u]) {
	$userRecord = $u;
	$user_found = '1';

}
}

// If no user is found, login has failed. Re-direct back to login.
if(!$user_found) {
header('Location: login.php?value=fail');
exit;
}

// If usernames don't match, login has failed. Re-direct back to login.
if($usernames[$userRecord] !=$username) {
header('Location: login.php?value=fail');
exit;

// If passwords don't match, login has faled. Re-direct back to login.
} else if($passwords[$userRecord] !=$password) {
header('Location: login.php?vale=fail');
exit;

// If usernames and passwords match, login succeeded. Re-direct to player specific page.
} else {
$_SESSION['username'] = $username;
header("Location: $page[$userRecord]");
exit;
}
?>

 

wont even allow the login, it wont move page, just says login failed... :S

 

thanks for the try tho!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.