使用用户名/密码登录PHP [英] PHP Login with Username/Password

查看:90
本文介绍了使用用户名/密码登录PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何编辑下面的PHP以允许其他或多个用户名和密码.例如,密码为JHFDE3的User1,密码为LKFW34的User2,等等.据我所读,关联数组可能是最好的.没有数据库,它被硬编码为简单地登录并能够查看php页面.

How would I edit the PHP below to allow for additional or multiple user names and passwords. For example User1 with Password JHFDE3, User2 with Password LKFW34, etc. From what I have read, an associative array would likely be best. There is no database, it is hardcoded to simply login and be able to view a php page.

对于下面的特定代码,我如何将其设置为关联数组?谢谢.

For the particular code below, how would I make it into an associative array? Thank you.

<?php
session_start(); //initiates the sessions
if ($_POST['submit']) //checks to make sure the login form has been submitted
    {
    $user = "XXXXX";
    $pass = "XXXXXXX";
    if ($_POST['pass'] == $pass && $_POST['user'] == $user) //checks if the password submitted by the user matches the password stored in the $pass variable
        {
        $_SESSION['access'] = 1; //if login is successful create a session that can be authenticated 
        header("Location: " . $_SERVER['PHP_SELF']); //reload the page. The page will now not load the login form (see the first if condition)
    } else //if password is incorrect reload the login form
        {
        header("Location: " . $_SERVER['PHP_SELF']);
    }
} else if (!$_SESSION['access']) //if the "access" session is not accessible show the form (not logged in)
    {
?>

推荐答案

尝试一下:

-------------------------- UPDATE -------------------- --------------

--------------------------UPDATE----------------------------------

<?php
session_start(); //initiates the sessions

//begin testing
$_POST['user'] = 'username1';
$_POST['submit'] = true;
$_POST['password'] = 'pass3';
//end  testing

if (isset($_POST['submit'])) //checks to make sure the login form has been submitted
    {

    $users = array('username1','username2','username3');
    $passwords = array('pass1','pass2','pass3' );
    if(in_array($_POST['user'], $users))
    {
       $key = array_search($_POST['user'], $users);

       if($passwords[$key]==$_POST['password'])
       {
         $_SESSION['access'] = 1; //if login is successful create a session that can be authenticated 
         //header("Location: " . $_SERVER['PHP_SELF']);
         echo "welcome back ".$_POST['user'];
       } else //if password is incorrect reload the login form
        {
         //header("Location: " . $_SERVER['PHP_SELF']);
          echo "Password incorrect, error, redirecting to login";
        }


    }

    }
else 
{
  echo "Login form";
}
?>

输出:

Password incorrect, error, redirecting to login

但是,如果将$ _POST ['password']的值更改为"pass1",则如下所示:

But if you change the value of $_POST['password'] to 'pass1', like this:

$_POST['password'] = 'pass1';

您有以下输出:

welcome back username1

Saludos;)

这篇关于使用用户名/密码登录PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆