简体   繁体   中英

How can I split my Login process into functions?

I'm currently using a modified version of a login script I found online. Can anybody suggest some ways of modularizing the code into functions?

Here is the code for the login page:

<?php
include("db.php");
include("login_fns.php");
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST")
{
// username and password sent from Form
$username=mysql_real_escape_string($_POST['username']); 
$password=mysql_real_escape_string($_POST['password']); 
$password=md5($password);
$sql="SELECT * FROM client_login WHERE Username='$username' and Password='$password'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
$row = mysql_fetch_array($result);
$client_ref = $row['Client_ref'];
$user_level = $row['user_level'];
// If result matched $username and $password, table row must be 1 row
if($count==1)
{

$_SESSION['user_level'] = $user_level;
$_SESSION['client_ref'] = $client_ref;
$_SESSION['user'] = $username;

if ($user_level == '1') {
header('Location: admin.php');
} else {

header('Location: myaccount.php');
}
}
else 
{
echo "Error logging in!";
}
}
?>
<form action="login.php" method="post">
<label>UserName :</label>
<input type="text" name="username"/><br />
<label>Password :</label>
<input type="password" name="password"/><br/>
<input type="submit" value=" Login "/><br />
</form>

Ideally, I'd like a function for the user account search and the session setting. I previously tried to copy snippets of this code into a separate php functions file, but it didn't seem to work.

What do you think about this? :)

The function

<?php
function checkLogin($username, $password) {
    global $bd;

    $returnArray=array();
    $username=mysqli_real_escape_string($bd, $username);
    $password=md5($password);

    $getUser=mysqli_query($bd, "SELECT `Client_ref`,`user_level` FROM client_login WHERE Username='$username' and Password='$password'");
    $arrayUser=mysqli_fetch_array($getUser);

    if(mysqli_num_rows($getUser) == 0)
    {
        $returnArray['error']='true';
        $returnArray['errormsg']='User not found in the database.';

        return $returnArray;
    }

    $returnArray['Client_ref']=$row['Client_ref'];
    $returnArray['user_level']=$row['user_level'];

    return $returnArray;
}
?>

Rest of the code

<?php
include("db.php");
include("login_fns.php");
session_start();

if($_SERVER["REQUEST_METHOD"] == "POST")
{
    $username=$_POST['username']; 
    $password=$_POST['password']; 

    $loginArray=checkLogin($username, $password);

    if(!isset($loginArray['error']))
    {
        $_SESSION['user_level'] = $loginArray['Client_ref'];
        $_SESSION['client_ref'] = $loginArray['user_level'];
        $_SESSION['user'] = $username;

        if($loginArray['user_level'] == '1')
        {
            header('Location: admin.php');
        }
        else
        {
            header('Location: myaccount.php');
        }
    }
    else 
    {
        echo "Error logging in!";
        echo "The detailed error message is: ".$returnArray['errormsg'];
    }
}
?>
<form action="login.php" method="post">
    <label>UserName :</label>
    <input type="text" name="username"/><br />

    <label>Password :</label>
    <input type="password" name="password"/><br/>

    <input type="submit" value=" Login "/><br />
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM