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>

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>

try this:

    <?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>";

    ?>
    <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>

the problem with your code is that you want the header function after html output