簡體   English   中英

需要幫助弄清楚為什么我的php代碼惠特會話inst工作

[英]Need help figuring out why my php code whit sessions inst working

我目前正試圖自己做一個登錄代碼,我無法弄清楚會話如何工作。 如果有人可以檢查我的代碼並說是錯誤,那我將非常有責任。

抱歉,由於缺乏組織,第一次在此發布。

對不起,我的英語(不是我的母語)是葡萄牙語。

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

(對不起我的英語不好)

如果要在文件中使用$ _SESSION變量,則必須在該文件的開頭以及任何輸出之前編寫session_start()(如Koala Yeung所說)。

somefile.php:

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

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

$foo = $_SESSION['foo'];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM