dongmu2517 2016-03-16 07:54
浏览 46
已采纳

PHP:任何人都知道Facebook页面的会话处理吗?

Problem : User is already logged in so when I type local.test_php.com/form/

It takes me to Login page. local.test_php.com/form/index.php

But i want it to take it to my home page instead.(http://local.test_php.com/form/home.php)

what i want is like what facebook does when we type facebook.com (already logged in before) we r redirected to the news feed ... but in my case i get redirected to Login page(even if i'm already logged in) so i have to login again or type the url to get to the home page

I don't know how to customize sessions to always redirect to home page whenever user is logged in. It should not take me to login page in any condition.

Home page looks like this:

<!DOCTYPE html>
<html>
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>

<head>
    <title>My first PHP Website</title>
</head>
<body>
<h2 align="center">Home Page</h2>
<?php
session_start();
if(isset($_SESSION['user'])){

}else{
    header("location: index.php");
}

$user = $_SESSION['user'];
echo "<br>";
echo "WELCOME ".$user." ";
echo "<br>";
echo "Do you want to ";
print '<a href="logout.php" align="left">logout</a>';
echo " ?";
echo "<br>";

?>
</body>
</html>
  • 写回答

2条回答 默认 最新

  • douqixia7942 2016-03-16 08:35
    关注

    I rewrote your code a littlebit, so it's more readable now.

    First of all, when you want to use header function on your page, or start a session, you should do it before anyhing in the output buffer.

    As you see, I removed the echo things, if you use an IDE as an editor, syntax highlighting help you to read the code, and be sure, it is valid.

    The second thing, if you want to "remember" for user, when user closes all the browsers what stored the session variables, then you need to give a shot for cookies. But first, fix the code.

    <?php
    session_start();
    if (!isset($_SESSION['user'])) {
        header("location: index.php");
    }
    ?><!DOCTYPE html>
    <html>
        <script src="//code.jquery.com/jquery-1.9.1.js"></script>
        <script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2 align="center">Home Page</h2>
    <?php
        if (!empty($_SESSION['user'])) {
            ?>
            <p>WELCOME <?php echo $_SESSION['user']; ?></p>
            <p>Do you want to <a href="logout.php" align="left">logout</a>?</p>
            <?php
        }
    ?>
        </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)