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条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行