需要帮助了解为什么我的php代码whit sessions inst工作吗

需要帮助了解为什么我的php代码whit sessions inst工作吗,php,html,session,Php,Html,Session,我目前正在尝试自己做一个登录代码,我不知道如何在会话中工作。如果有人能检查我的代码并说是错误,我将不胜感激 很抱歉,第一次在这里发布时缺少组织 对不起,我的英语(不是我的母语),我是葡萄牙语 -index.php <html> <head> <?php session_start(); ?> <title> Login Page </title> </head> <

我目前正在尝试自己做一个登录代码,我不知道如何在会话中工作。如果有人能检查我的代码并说是错误,我将不胜感激

很抱歉,第一次在这里发布时缺少组织

对不起,我的英语(不是我的母语),我是葡萄牙语

-index.php
    <html>
    <head>
    <?php session_start(); ?>
    <title> Login Page   </title>

    </head>

    <body>

    <form action="login2.php" method="post">

    <table width="200" border="0">
    <tr>
    <td>  UserName</td>
    <td> <input type="text" name="user" > </td>
    </tr>
    <tr>
    <td> PassWord  </td>
    <td><input type="password" name="pass"></td>
    </tr>
    <tr>
    <tr>
    <td> Email </td>
    <td><input type="email" name="email"></td>
    </tr>
    <tr>
    <td> <input type="submit" name="login" value="LOGIN"></td>
    <td><a href="logout.php">Logout</a></td>
    </tr>
    </table>
    </form>

    </body>
    </html>

     Home.php
    <?php   
    require_once 'database.php';  
    $res=mysql_query("SELECT * FROM users WHERE id=".$_SESSION['user']);
    $userRow=mysql_fetch_array($res); ?>
    <html>
    <head>
    <title> Home </title>
    </head>
    <body>
    <?php
    if(!isset($_SESSION['use'])) 
    {
    header("Location:Login.php");  
    }
    echo $userRow['userEmail']; 
    echo "Login Success";
    echo "<a href='logout.php'> Logout</a> "; 
    ?>
    </body>
    </html>

    logout.php
    <?php
    session_start();
    echo "Logout Successfully ";
    session_destroy();   // function that Destroys Session 
    header("Location: Login.php");
    ?>

     database.php
    <?php
    // this will avoid mysql_connect() deprecation error.
    error_reporting( ~E_DEPRECATED & ~E_NOTICE );
    // but I strongly suggest you to use PDO or MySQLi.

    define('DBHOST', 'localhost');
    define('DBUSER', 'root');
    define('DBPASS', '');
    define('DBNAME', 'database_sof');

    $conn = mysql_connect(DBHOST,DBUSER,DBPASS);
    $dbcon = mysql_select_db(DBNAME);
    ?>



     login2.php
    <?php
    require_once 'database.php';
    if(isset($_SESSION['user']))  {
    header("Location:home.php"); 
    }
    if(isset($_POST['login']))   {
    $user = $_POST['user'];
    $pass = $_POST['pass'];
    $email = $_POST['email'];

    if(empty($user)){
    echo "Please enter your username.";}  
    if(empty($pass)){
    echo "Please enter your passoword.";}   
    if(empty($email)){
    echo "Please enter your email.";}  
    $res=mysql_query("SELECT id, username, password FROM users WHERE email='$email'");
    $row=mysql_fetch_array($res);
    $count = mysql_num_rows($res); 
    if( $count == 1 && $row['password']==$pass ) {
    $_SESSION['user'] = $row['id'];
    session_start();
    header("Location: home.php");} else {
    echo $user;
    echo "<br>";
    echo $pass;
    echo "<br>";
    echo $email;
    echo "<br>";
    echo $count;
    echo "<br>";
    echo $row['password'];
    echo "<br>";
    echo "Incorrect Credentials, Try again...";
    }}?>
-index.php
登录页面
用户名
密码
电子邮件
Home.php
家
logout.php
database.php
login2.php
(对不起我的英语)

如果要在文件中使用$\u SESSION变量,必须在该文件的开头写入SESSION\u start(),并在任何输出之前写入(正如考拉杨所说)

somefile.php:

<?php
session_start();
...
//now you can read or edit $_SESSION

$_SESSION['bar'] = "bar";

$foo = $_SESSION['foo'];

您使用的是什么版本的PHP?mysql_u.函数已被弃用并替换为
mysqli
。在任何HTML输出之前,您需要运行session_ustart()。@Koalayeng感谢您的帮助。