-2

I'm not able to start session_start () in php and I really can't understand the reason, I've tried a few things people suggested on forums, but the session_start() still does not work

index file where are not retrieving variables $ _SESSION:

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
    <title>Pagina1</title>
    <link rel="stylesheet" href="css/style.css"/>

</head>
<body>
    <article id="newPrincipal">
        <h1>Usuário id:<?php echo $_SESSION['userId']; ?></h1> 
    </article>
    <h1>Result:<?php echo "Usuário id:".$_SESSION['userId']; ?></h1>
</body>

</html>
<?php
echo '<pre>';
print_r($_SESSION['userId']);
echo '</pre>';

login file:

<?php

if(isset($_POST['login-submit'])){

    require 'dbh.inc.php';

   $users = $_POST['nome'];
    $mailuid = $_POST['mailuid'];
    $password = $_POST['pwd'];
    $token;
    if(empty($mailuid) || empty($password)){
        header("Location: ../header.php=emptyfields");
        exit();
    }
    else{
        $sql = "SELECT * FROM users WHERE Usuarios=? AND email=?";
        $stmt = mysqli_stmt_init($conn);
        if(!mysqli_stmt_prepare($stmt, $sql)){
            header("Location: ../index.php?error=sqlerror");
            exit();
        }
        else{
            mysqli_stmt_bind_param($stmt, "ss", $mailuid, $users);
            mysqli_stmt_execute($stmt);
            $result = mysqli_stmt_get_result($stmt);
            if($row = mysqli_fetch_assoc($result)){
                $pwdCheck = password_verify($password, $row['pwdUsers']);
                if($pwdCheck == false){
                    header("Location: ../header.php?error=wrongpwd");
                    exit();
                }
                else if($pwdCheck == true){
                    session_start();
                    $_SESSION['userId'] = $row['idUsers'];
                    $_SESSION['userId2'] = $row['uidUsers'];
                    $_SESSION['email'] = $row['emailUsers'];

                    header("Location: ../index.php?login=".$_SESSION['userId']);
                }
                else{
                    header("Location: ../login.php?error=wrongpwd");
                    exit();
                }
            }
        }
    }
}
else{
    header("Location: ../index.php");
}

I'm using an external server not the Xampp / Wamp, I do not know if this would imply anything ...

I tried every ways to retrieve some information using $ _SESSION in the index file, but I could not.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Kerolaine
  • 13
  • 3
  • Your session will only start in the 2nd body of code if all conditions are met. Enable error reporting and check for errors on the query. – Funk Forty Niner Nov 06 '18 at 15:38
  • `session_start();` **should always be at the TOP of your code** – RiggsFolly Nov 06 '18 at 15:43
  • And your `index.php` should be generating an error as when that runs there will be nothing in $_SESSION as no login has been done yet. Add `ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` to the top of your script. This will force any mysqli_ errors to generate an Exception that you can see on the browser as well as normal PHP errors – RiggsFolly Nov 06 '18 at 15:45
  • Riggsfolly and Funk Forty Nine thanks for replying. I did everything you asked me to, but it's still not working, you know the best way to find out if have any errors in the code ?? – Kerolaine Nov 06 '18 at 20:32

1 Answers1

0

To ensure that you are calling session_start(); and checking for possible session errors on both pages, make sure the first three lines of code on both pages are the following:

<?php
error_reporting(E_ALL); //will show session errors (if any)
session_start(); //starts the session and makes $_SESSION variables available

Then, in your login file, change the code where you set the $_SESSION variables to match the following:

...
else if($pwdCheck == true){
    session_start();
    $_SESSION['userId'] = $row['idUsers'];
    $_SESSION['userId2'] = $row['uidUsers'];
    $_SESSION['email'] = $row['emailUsers'];

    //test output
    echo "The $_SESSION['userID'] variable is set as: " & $_SESSION['userID'];

    exit;

    //comment the redirect out for now, so that you see the test output
    //header("Location: ../index.php?login=".$_SESSION['userId']);
}
...

In the comments, let me know if you get any errors and what the test output for the login file is.

Justin T.
  • 826
  • 1
  • 5
  • 13
  • Thank you for your help, Justin. I added session_start () at the top of the script, even though it is not working !! – Kerolaine Nov 06 '18 at 20:35
  • I just updated my solution again, I noticed another small error. Please try this and let me know if it is working. – Justin T. Nov 06 '18 at 21:38
  • the parameters were correct, I have no idea why the session does not work, when I ask to print the $ _SESSION ['userId'] in the login script it works, but when I ask to print in sript index it does not work – Kerolaine Nov 06 '18 at 22:14
  • At the end of your index file, it looks like you open the PHP tag, but you do not close it. Add the closing PHP tag (`?>`) to the very end of the index file. Also, try using `echo $_SESSION['userId'];` instead of `print_r($_SESSION['userId']);` – Justin T. Nov 07 '18 at 02:18
  • did not work. Anyway, thanks for your help, I give up! – Kerolaine Nov 07 '18 at 11:52
  • I am not giving up on you! I just updated my solution, we will get this working! – Justin T. Nov 07 '18 at 12:49
  • you are awesome ! But I think you have no problem with the code. I tried to see if I conceived a result using XAMPP and everything went well as I wanted, using XAMPP the $ session gave me the information I requested ... the problem was with the hosting server and it's already resolved. Anyway, thank you for helping me! <3 – Kerolaine Nov 07 '18 at 18:15
  • Excellent, I am glad you were able to get it working! Good luck with the rest of your project! – Justin T. Nov 07 '18 at 19:28
  • Thanks !!! ^_^b – Kerolaine Nov 07 '18 at 23:20