doujia7517 2017-03-14 17:40
浏览 54
已采纳

需要帮助搞清楚为什么我的PHP代码与会话不起作用

Im currently trying to do a login code by myself and i cant figure out how ot work whit session. If anyone can examine my code and say were are the errors i would be much obliged.

Sorry for the lack of organization first time posting here.

Sorry for my english(not my native language), im portuguese.

-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...";
    }}?>
  • 写回答

1条回答 默认 最新

  • douyue1481 2017-03-14 19:40
    关注

    (sorry for my english)

    if you want to use $_SESSION variable in your file, you must write session_start() at the beginning of that file AND before any output (as Koala Yeung said).

    somefile.php:

    <?php
    session_start();
    ...
    //now you can read or edit $_SESSION
    
    $_SESSION['bar'] = "bar";
    
    $foo = $_SESSION['foo'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!