Jump to content

How to construct a log-in system with multiple usernames and passwords


rotten69

Recommended Posts

Hey All,

 

I'm tryin to make a log-in system for multiple usernames and passwords, but I don't really know how many if statements i'd need for it.. I'm also a noob..

 

 

<?php

session_start();
$users = array("user1" =>"3202", "user2" =>"2002", "user3" =>"1061", "user4"=>"1400", "user5"=>"1001");

if($_REQUEST['username'] == "infs" && $_REQUEST['password'] == "3202"){

	$_SESSION['username'] = "user1" ;
	$_SESSION['password'] = "3202" ;

	$_SESSION['username'] = "user2" ;
	$_SESSION['password'] = "2002" ;

	$_SESSION['username'] = "user5" ;
	$_SESSION['password'] = "1001" ;

	$_SESSION['username'] = "user3" ;
	$_SESSION['password'] = "1061" ;

	$_SESSION['username'] = "user4" ;
	$_SESSION['password'] = "1400" ;
	header("Location: home.php ");
}else{

 

 

After checking if the matching username and password exist in my array then save them in a session...

 

 

What's the best way of doing it?

You are definitely way off.  Why don't you start by explaining what your current code is doing, and why you are checking $_REQUEST['username'] == "infs"?  If this is reached via a POST then don't use $_REQUEST. 

I'm just trying to check if the username and password are matched with the ones in the array then a new session is on.

 

if($_REQUEST['username'] == "user1" && $_REQUEST['password'] == "3202") 

then take them to the home page else stay on the same page..   so, having multiple usernames and passwords, how would you check for that?
Would you do an if statement for each username/password?

Again, don't use $_REQUEST, use $_POST.

 

Here's some code that should help you understand how to approach this:

 

 

session_start();
$users = array("user1" =>"3202", "user2" =>"2002", "user3" =>"1061", "user4"=>"1400", "user5"=>"1001");

if (isset($_SESSION['username'])) {
  // already logged in.
  header('Location: home.php');
  exit();
} elseif (isset($_POST['username']) && isset($_POST['password']) && array_key_exists($_POST['username'], $users) && $users[$_POST['username']] == $_POST['password'])  {
  $_SESSION['username'] = $_POST['username'];
  $_SESSION['password'] = $users[$_SESSION['username']];
  header('Location: home.php');
  exit();

} else {
   // header() back to login form
   exit();
}

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.