为什么PHP在登录成功后仍然显示?

huangapple go评论73阅读模式
英文:

why php always display after login sucessful?

问题

To fix the issue and pass the loginSuccess variable from your PHP code to your HTML, you can use PHP to generate JavaScript code within your HTML file. Here's how you can modify your home.html file to achieve this:

<!DOCTYPE html>
<html lang="en">
<head>
    <!-- ... (your existing head content) ... -->
</head>
<body>
    <header>
        <!-- ... (your existing header content) ... -->
    </header>

    <div class="wrapper">
        <!-- ... (your existing wrapper content) ... -->
    </div>

    <script>
        <?php
            echo "var loginSuccess = " . ($loginSuccess ? 'true' : 'false') . ";";
        ?>

        $(document).ready(function() {
            if (loginSuccess) {
                $('#welcome').show();
                $('#loginForm').hide();
            } else {
                $('#welcome').hide();
                $('#loginForm').show();
            }
        });
    </script>

    <div class="opening" id="openingSection">
        <!-- ... (your existing opening content) ... -->
    </div>

    <script type="module" src="https://unpkg.com/ionicons@5.5.2/dist/ionicons/ionicons.esm.js"></script>
    <script nomodule src="https://unpkg.com/ionicons@5.5.2/dist/ionicons/ionicons.js"></script>
</body>
</html>

With this modification, the loginSuccess variable will be passed from your PHP code to the JavaScript code in your HTML, allowing you to control the visibility of the welcome and loginForm elements based on the value of loginSuccess.

英文:

here's my login.php file, i want to make this $loginSucess only give variable to home.html instead showing the login php (which is turned to be blank page after sucessfully login)

&lt;?php
    $email = $_POST[&#39;email&#39;];
    $password = $_POST[&#39;password&#39;];
    $otp_code = intval($_POST[&#39;otp_code&#39;]);
    $passwordMismatch = false;
    $loginSuccess = false;

    // Database configuration
    $host = &#39;localhost&#39;;
    $dbName = &#39;-&#39;;
    $username = &#39;-&#39;;
    $dbPassword = &#39;-&#39;;

    // Create a database connection
    $con = new mysqli($host, $username, $dbPassword, $dbName);

    // Check if the connection was successful
    if ($con-&gt;connect_error) {
        die(&quot;Database connection failed: &quot; . $con-&gt;connect_error);
    } else {
        $stmt = $con-&gt;prepare(&quot;SELECT * FROM account_identity WHERE email=?&quot;);
        $stmt-&gt;bind_param(&quot;s&quot;, $email);
        $stmt-&gt;execute();
        $stmt_result = $stmt-&gt;get_result();
        if($stmt_result-&gt;num_rows &gt; 0) {
            $data = $stmt_result-&gt;fetch_assoc();
            if($data[&#39;password&#39;] === $password &amp;&amp; $data[&#39;otp_code&#39;] === $otp_code) {
                 $loginSuccess = true;
            } else {
                $passwordMismatch = true;
            }
        } else {
            $passwordMismatch = true;
        }
    }
?&gt;

and here's my home.html

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;

&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width,initial-scale=1.0&quot;&gt;
    &lt;title&gt;Arcane City Roleplay&lt;/title&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;css/style.css&quot;&gt;
    &lt;link rel=&quot;icon&quot; href=&quot;images/logo.png&quot; type=&quot;image/x-icon&quot;&gt;
    &lt;script src=&quot;https://code.jquery.com/jquery-3.6.0.min.js&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;script/script.js&quot; defer&gt;&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
    &lt;header&gt;
        &lt;h2 class=&quot;logo&quot;&gt;ARCANE CITY&lt;/h2&gt;
        &lt;nav class=&quot;navigation&quot;&gt;
            &lt;a href=&quot;home.html&quot;&gt;Home&lt;/a&gt;
            &lt;a href=&quot;#&quot;&gt;Statistics&lt;/a&gt;
            &lt;a href=&quot;#&quot;&gt;Gallery&lt;/a&gt;
            &lt;a href=&quot;#&quot;&gt;Donate&lt;/a&gt;
            &lt;a href=&quot;#&quot;&gt;Status&lt;/a&gt;
            &lt;a href=&quot;#&quot;&gt;About Us&lt;/a&gt;
            &lt;button class=&quot;btnLogin-popup&quot; onclick=&quot;toggleLogin()&quot;&gt;Login&lt;/button&gt;
        &lt;/nav&gt;
    &lt;/header&gt;

    &lt;div class=&quot;wrapper&quot;&gt;
        &lt;span class=&quot;icon-close&quot; onclick=&quot;toggleSections()&quot;&gt;
            &lt;ion-icon name=&quot;close&quot;&gt;&lt;/ion-icon&gt;
        &lt;/span&gt;
        &lt;div class=&quot;form-box login&quot; id=&quot;loginForm&quot;&gt;
            &lt;h2&gt;Login&lt;/h2&gt;
            &lt;form action=&quot;login.php&quot; method=&quot;post&quot;&gt;
                &lt;div class=&quot;input-box&quot;&gt;
                    &lt;span class=&quot;icon&quot;&gt;&lt;ion-icon
                    name=&quot;mail&quot;&gt;&lt;/ion-icon&gt;&lt;/span&gt;
                    &lt;input type=&quot;email&quot; id=&quot;email&quot; name=&quot;email&quot; required&gt;
                    &lt;label&gt;Email&lt;/label&gt;
                &lt;/div&gt;
                &lt;div class=&quot;input-box&quot;&gt;
                    &lt;span class=&quot;icon&quot;&gt;&lt;ion-icon
                    name=&quot;lock-closed&quot;&gt;&lt;/ion-icon&gt;&lt;/span&gt;
                    &lt;input type=&quot;password&quot; id=&quot;password&quot; name=&quot;password&quot; required&gt;
                    &lt;label&gt;Password&lt;/label&gt;
                &lt;/div&gt;
                &lt;div class=&quot;remember&quot;&gt;
                    &lt;label&gt;&lt;input type=&quot;checkbox&quot;&gt;
                    Remember me?&lt;/label&gt;
                &lt;/div&gt;
                &lt;div class=&quot;input-box&quot;&gt;
                    &lt;input type=&quot;text&quot; id=&quot;otp_code&quot; name=&quot;otp_code&quot; pattern=&quot;[0-9]{4}&quot; maxlength=&quot;4&quot; required&gt;
                    &lt;label&gt;OTP&lt;/label&gt;
                &lt;/div&gt;
                &lt;button type=&quot;submit&quot; value=&quot;Login&quot; name=&quot;login&quot; class=&quot;btn&quot;&gt;Login&lt;/button&gt;
                &lt;div class=&quot;login-register&quot;&gt;
                    &lt;p&gt;Don&#39;t have an account? &lt;a href=&quot;#&quot; class=&quot;register-link&quot;&gt;Register
                    &lt;/a&gt;&lt;/p&gt;
                &lt;/div&gt;
            &lt;/form&gt;
        &lt;/div&gt;

        &lt;div class=&quot;form-box welcome&quot; id=&quot;welcome&quot;&gt;
            &lt;h2&gt;Welcome Back&lt;/h2&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    
    &lt;script&gt;
        $(document).ready(function() {
            var loginSuccess = &lt;?php echo ($loginSuccess ? &#39;true&#39; : &#39;false&#39;); ?&gt;;

            if (loginSuccess) {
                $(&#39;#welcome&#39;).show();
                $(&#39;#loginForm&#39;).hide();
            } else {
                $(&#39;#welcome&#39;).hide();
                $(&#39;#loginForm&#39;).show();
            }
        });
    &lt;/script&gt;
    
    &lt;div class=&quot;opening&quot; id=&quot;openingSection&quot;&gt;
        &lt;h1&gt;ARCANE CITY&lt;/h1&gt;
        &lt;h2&gt;GTA 5 ROLEPLAY&lt;/h2&gt;
        &lt;button class=&quot;btnConnect&quot; onclick=&quot;window.location.href=&#39;https://www.wikipedia.org&#39;;&quot;&gt;CONNECT TO SERVER&lt;/button&gt;
    &lt;/div&gt;

    &lt;script type=&quot;module&quot; src=&quot;https://unpkg.com/ionicons@5.5.2/dist/ionicons/ionicons.esm.js&quot;&gt;&lt;/script&gt;
    &lt;script nomodule src=&quot;https://unpkg.com/ionicons@5.5.2/dist/ionicons/ionicons.js&quot;&gt;&lt;/script&gt;
&lt;/body&gt;

&lt;/html&gt;

how to fix this issues? just to let variable run and give variable back to the html instead showing php file

答案1

得分: 2

为了实现将$loginSuccess变量传递回home.html文件,而不是显示login.php文件的期望行为,您可以使用会话变量。

  1. 在您的login.php文件中,在向浏览器发送任何输出之前添加以下代码来启动会话:
session_start();
  1. 使用$_SESSION超全局变量将$loginSuccess的值设置为会话变量。将$loginSuccess = true;这一行替换为:
$_SESSION['loginSuccess'] = true;
  1. 在成功登录后将用户重定向回home.html文件。在设置会话变量后添加以下代码:
header("Location: home.html");
exit();
  1. home.html文件中,删除检查loginSuccess值的PHP代码,因为我们现在将从会话变量中检索它。

  2. home.html中的<script>标签中,更新代码以从会话变量中检索loginSuccess的值,并根据该值显示/隐藏适当的部分。将现有的JavaScript代码替换为以下内容:

$(document).ready(function() {
    var loginSuccess = <?php echo (isset($_SESSION['loginSuccess']) && $_SESSION['loginSuccess']) ? 'true' : 'false'; ?>;
    
    if (loginSuccess) {
        $('#welcome').show();
        $('#loginForm').hide();
    } else {
        $('#welcome').hide();
        $('#loginForm').show();
    }
});

通过这些更新,当登录成功时,login.php文件将设置loginSuccess会话变量并将用户重定向回home.html。然后,在home.html中,JavaScript代码将从会话变量中检索loginSuccess的值,并根据该值显示/隐藏适当的部分。

英文:

To achieve the desired behavior of passing the $loginSuccess variable back to the home.html file instead of displaying the login.php file, you can make use of session variables.

  1. In your login.php file, start a session by adding the following code before any output is sent to the browser:
session_start();
  1. Set the value of $loginSuccess as a session variable using the $_SESSION superglobal. Replace the line $loginSuccess = true; with:
$_SESSION[&#39;loginSuccess&#39;] = true;
  1. Redirect the user back to the home.html file after successful login. Add the following code after setting the session variable:
header(&quot;Location: home.html&quot;);
exit();
  1. In the home.html file, remove the PHP code that checks the value of loginSuccess as we will now retrieve it from the session variable.

  2. Inside the &lt;script&gt; tag in home.html, update the code to retrieve the loginSuccess value from the session variable and show/hide the appropriate sections based on the value. Replace the existing JavaScript code with the following:

$(document).ready(function() {
    var loginSuccess = &lt;?php echo (isset($_SESSION[&#39;loginSuccess&#39;]) &amp;&amp; $_SESSION[&#39;loginSuccess&#39;]) ? &#39;true&#39; : &#39;false&#39;; ?&gt;;
    
    if (loginSuccess) {
        $(&#39;#welcome&#39;).show();
        $(&#39;#loginForm&#39;).hide();
    } else {
        $(&#39;#welcome&#39;).hide();
        $(&#39;#loginForm&#39;).show();
    }
});

With these updates, when the login is successful, the login.php file will set the loginSuccess session variable and redirect the user back to home.html. Then, in home.html, the JavaScript code will retrieve the value of loginSuccess from the session variable and show/hide the appropriate sections accordingly.

huangapple
  • 本文由 发表于 2023年7月10日 12:20:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76650663.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定