简体   繁体   中英

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

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

(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'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM