Home>
Current status

I am currently creating a web service using PHP.
When I was editing the login function and suddenly operating the DB, I wanted to make it a function, so I made it a function, but as soon as I made it a function, it stopped working normally.

I want to achieve

As the title says, "If i make the process a function, it will not work properly."
So I want to make it work normally even if the process is made into a function.
Also, "normal operation" means successful login.

Specifically,

<? php
//Login_exec.php (It is a function file of the login function)
    // File that requires_once such as function.php
    require_once "../libs/init.php";
// Display if there is an error
    if (isset ($_GET ['error'])) {
        echo $_GET ['error'];
    }
    if (isset ($_ POST ['login'])) {
        $email = $_POST ['email'];
        $_SESSION ['email'] = $_POST ['email'];
        $password = $_POST ['password'];
        if (! filter_var ($email, FILTER_VALIDATE_EMAIL)) {
            header ('Location: login.php? Error = The value entered is invalid.');
            return false;
        }

        // // Search the POSTed email address in the DB
        try {
            // Search_Email ($email);← This is a function of the following processing
        // from here
            $pdo = db_connect ();
            $stmt = $pdo->prepare ('SELECT * FROM users WHERE email =?');
            $stmt->execute ([$email]);
            $row = $stmt->fetch (PDO :: FETCH_ASSOC);
            // This is the process of making a function
        } catch (\ Exception $e) {echo $e->getMessage ();
        }
        // Check if email exists in DB
        if (! isset ($row ['email'])) {
            header ('Location: login.php? Error = wrong email address or password.');
            return false;
        }
        // Pass the email address to session after confirming the password
        if (password_verify ($password, $row ['password'])) {
            session_regenerate_id (true);// Generate and replace session_id
            $_SESSION ['user_id'] = $row ['user_id'];
            $_SESSION ['USERNAME'] = $row ['username'];
            $user_id = $_SESSION ['user_id'];
            ?>           Login was successful.           <? php
            header ("Location: calendar.php");
        } else {
            header ("Location: login.php? Error = wrong email address or password.");
            return false;
        }
    }

<? php
//function.php (a file that summarizes functions)
// email search
function Search_Email ($email) {
    $pdo = db_connect ();
    $stmt = $pdo->prepare ('SELECT * FROM users WHERE email =?');
    $stmt->execute ([$email]);
    $row = $stmt->fetch (PDO :: FETCH_ASSOC);
}

I made the process of login_exec.php a function in this file and required_once in init.php, but when I wrote the process directly in login_exec.php without making it a function, it worked normally, but I made it a function When I call it, it doesn't work properly for some reason.

Related source code
// login.php (Login function display file)<? php
require_once "../libs/init.php";
if (isset ($_SESSION ['user_id'])) {
    header ("Location: calendar.php");
}
$smarty = getSmarty ();
if (isset ($_GET ['error'])) {
    $smarty->assign ('error', $_GET ['error']);
}
$smarty->display ('login.tpl');
// login.tpl (Smarty file for login function)<form action = "login_exec.php" method = "POST">        <p>Email address</p>    <input type = "email" name = "email"> <br>        <p>Password</p>    <input type = "password" name = "password"> <br><button type = "submit" name = "login">Login</button></form>{if isset ($error)}
  <p>* {$error}</p>{/ if}  <p>Click here if you do not have an account</p>   Create an account
What I tried

Add argument to Search_Email ()
→ Until now, $email was undefinded in the function, so I thought that if I added $email, I could receive the email address normally, but the result was the same and it failed.

---
that's all.
If anyone knows, please answer.