douhe1002 2015-11-25 09:24
浏览 95
已采纳

登录后如何留在页面上并注销

how do i create the below php that the user stays on the index page after logging in ? It seems it will direct the user to the logonprocess.php after clicking the submit button.

I'm also trying to find out how will the logout button appear after the user login successfully. The logout will also need to work the same as login which will stay on the same page.

I have read that ajax was one way but i have not yet read or understand ajax. I'm still trying to learn on the php portion first.

Index.php
           <?php
            ini_set("session.save_path", "sessionData");
            session_start();
            ?>

 <?php if (!isset($_SESSION['uName'])) { ?>
    <form method="post" action="logonProcess.php">
    <div>Username <input type="text" name="userName" placeholder="Username"></div>
    <div>Password <input type="password" name="pwd" placeholder="Password"></div>
    <div><input type="submit" value="Logon"></div>
    </form>
<?php } else { }?>



<?php if (isset($_SESSION['uName'])) { 
        $username = $_SESSION['uName'];     
    echo "<p>Welcome $username</p>
";
?>
        <a href="logout.php">Logout</a>

<?php } else { }?>

Logout.php

<?php
session_start();
if(session_destroy()) // Destroying All Sessions
{    
header("Location: index.php"); // Redirecting To Home Page
}
?>       
  • 写回答

4条回答 默认 最新

  • dshxbrzgi090608692 2015-11-25 09:40
    关注

    If you want to redirect somewhere after a certain script has been executed you could ofcourse always use PHP's header() function which allows you to specify a Location which would look like this

    header('Location: index.php');

    After that your part two of the question is "How do I remove the logout button when the user login successfully?" I think with login you must mean logout since you'll want to be able to actually logout once logged in.

    To do this you check wether or not a $_SESSION

    A $_SESSION in PHP is simply an array containing values that are remembered across page reloads so as you can imagine - it is a very good place to store your user ID.

    The reason that usually just an ID is saved is so that while a hacker might still be able to compromise your users' cookie he / she will not be able to see any data he / she shouldn't have like a password, email address, phone number etcetera so all damage done will be on the website itself, not the users personal life ^.^

    When you create a $_SESSION in PHP you simply set it in your logonProces.php file after all the authentication checks for the user passed.

    This would look something like this (semi-psuedo code)

    if ($user_verified_in_db) {
        $_SESSION['user'] = $user['ID']; //note - non of this will probably exist yet in your script, DONT use it its an EXAMPLE.
        header('Location: index.php');
    }
    

    The above snippet should be placed somewhere appropiate in the procesLogon.php file so that the session will be set.

    Now in HTML you'll have a link somehwere right?

    <a href="logout.php" class="..." id="...">Logout</a>
    

    Imagine that is your link being displayed somewhere on the page, now what you want to do is check if the $_SESSION['user'] is set using isset().

    Your code would look something like this:

    <?php if (isset($_SESSION['user'])) { ?>
        <a href="logout.php" class="..." id="...">Logout</a>
    <?php } ?>
    

    this will check if the session is set or not, if it isn't set it won't display the link, if it is it will since you'll need an option to logout.

    NOTE this is psuedo code - you still have to build this construction using your variables and your login script, my tiny piece of code doesn't do anything for you at that except show you an example of how this is commonly handled.

    Good luck!

    EDIT (5-11-2015)

    As per the comment of the OP,

    If you want to hide items in general, like the logout link example above, all you have to do is wrap the divs you want to hide in the if statement.

    e.g.

    <?php if (isset($_SESSION['user'])) { ?>
        <!-- this can be any HTML element showing stuff for logged in users. -->
    <?php } ?>
    

    when you wrap elements within this if statement - if you check the expression: isset($_SESSION['user']) - it will evaluate to true if $_SESSION['user'] is set which you are in your login script.

    You can keep reusing this check whenever and wherever you need to show / hide elements from the user.

    if you would put a ! (exclamation mark) in front of the expression so that it turns out like this: !isset($_SESSION['user']) you reverse the process so if you have the following statement

    <?php if (isset($_SESSION['user'])) { ?>
        <!-- everything here is shown when user is logged in -->
    <?php } else { ?>
        <!-- everything here is shown when user is logged out -->
    } ?>
    

    this is the positive if check checking if your user is logged in or not, you can decide to put in the else for what to do when the user isn't logged in but you can also modify the expression slightly to reverse or invert the situation e.g.

    <?php if (!isset($_SESSION['user'])) { ?>
        <!-- everything here is shown when user is logged out -->
    <?php } else { ?>
        <!-- everything here is shown when user is logged in -->
    } ?>
    

    for instance. This will allow you to gain control over what users see on your webpages, use them whenever you need to show or hide something.

    Also note that the else clause is ofcourse, optional and doesn't have to be included, you can use the ! example without the else as well as the one without the exclamation mark.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝