Search code examples
phpsessionsession-variables

PHP $_SESSION issue


I am experiencing issues with $_SESSION, I'll paste the code and explain.

I have a index which includes

<div class="confidential__field text-align-center">
    <table>
        <tbody>
            <tr>
                <td><label for="email">Email</label></td>
                <td><input type="text" id="email" name="email"></input></td>
            </tr>
            <tr>
                <td><label for="pwd">password</label></td>
                <td><input type="password" id="pwd" name="pwd"></input></td>
            </tr>
            <tr>
                <td><a href="?action=register">Don't have an account ?</a></td>
                <td><button form="form" id="login">Send</button></td>
            </tr>
        </tbody>
    </table>
</div>

index.php we have this(shortened version, and yes, I have session_start() at the begining of the index.php):

<?php 
session_start();
include("_include/_stuff/config.php");?>
<!doctype html>
<html>
    <?php include("_general/head.php");?>
    <body>
         <?php 
        include("_include/login.php");
        if(isset($_SESSION['ID']) && !empty($_SESSION['ID']))include("_include/header.php");
    ?>
</body>
</html>

and then the script:

$(document).ready(function(){
        $("#login").click(function(){
                            $.post("_ajax/clientMain.php",{
                                login: 1,
                                email: $("input#email").val(),
                                pwd: $("input#pwd").val()
                            },
                                function(data){
                                    if(data==1){
                                        //alert(data);
                                        // alert("SESSION SET");
                                        location="index.php";
                                    }else{
                                        alert(data);
                                    }
                                }
                            )
                        });
});

and then the actual php to check:

session_start();    
if(isset($_POST['login']) && !empty($_POST['login'])){
        $conn = db_connect();

    $email = db_quote($_POST['email']);
    $pwd = db_quote($_POST['pwd']);

    $query_attendance = mysqli_query($conn, "select * from user where email=$email and password=$pwd");
    if(mysqli_num_rows($query_attendance) > 0){
        $credentials = mysqli_fetch_array($query_attendance, MYSQLI_ASSOC);
        if(!empty($credentials)){
            $_SESSSION['ID'] = $credentials['userID'];
            echo 1;
        }else{
            echo 82;
        }
    }else{
        echo 45;
    }
}else{
    echo 111;
}

I am running on XAMPP(localhost) on windows 10.

NOTE: I am not encoding password because I am just doing basic testing in which I am hardly failling!

EDIT: After attrbuiting the ID to the session, the session is not saved to the index.php, because if I do a var_dump($_SESSION) or echo $_SESSION["ID"] I recieve nothing, but if I respond with data in the login script and instead for echo 1 I would have echo $_SESSION["ID"] then I get the session/id, but It is not being transfered somehow to the index.php page!


Solution

  • You have a typo, check if it's only here in your question's code or in your actual code as well:

    $_SESSSION['ID'] = $credentials['userID'];
    

    Should be:

    $_SESSION['ID'] = $credentials['userID'];
    

    You have an extra S