HTML PHP Display username after success login

后端 未结 8 1591
执念已碎
执念已碎 2020-12-06 03:15

I have a

inside my onlinestore.html file which is my menu that contain of Login/Register. What I want is after the success login, the
相关标签:
8条回答
  • 2020-12-06 03:41

    Well, there is a lot of code missing.

    Where is your login script?

    First, you need a <form> with the input fields (login and password) and a submit button.

    For example:

    <form action='login.php' method='post'>
    <input type='text' name='loginname'></input>
    <input type='password' name='loginpassword'></input>
    <input type='submit'>Submit</input>
    </form>
    

    Your login.php

    <?php 
    session_start();
    // Dont forget to check your variables
    $loginname = $_POST["loginname"];
    $password = $_POST["loginpassword"];
    // Check the correct login (for example with a database)
    if ($login_correct) {
        $_SESSION["username"] = $loginname;
        $_SESSION["logged"] = true;
        header("index.html");
        exit;
    else {
        $_SESSION["logged"] = false;
        header("registerform.html");
        exit;
    
    
       }
    }
    ?>
    

    In your index.html you can do:

    <?php 
    session_start();
    if($_SESSION['logged'] == true){ 
        echo $_SESSION["username"];
        echo '<a href="logout.php"><span>Logout</span></a></li>';
    }
    else {
        echo '<a href="registerform.html"><span>Login/Register</span></a></li>';
    }
    ?>
    
    0 讨论(0)
  • 2020-12-06 03:48

    I think your PHP should look something like that:

    <?php 
    session_start();
    username = $_SESSION["username"]
    if($_SESSION['logged'] == true) { 
        echo '<a href="logout.php"><span>' . username. 'Logout</span></a></li>';
        }
    else {
        echo '<a href="registerform.html"><span>Login/Register</span></a></li>';
    }
    ?>
    

    Use either brackets in both if and else or use none. By the way you don't need an elseif if you're only checking a boolean. (there are only 2 possibilities)

    0 讨论(0)
提交回复
热议问题