2

I have sample page the content of it is the following code:

<?php 
session_start();
if (isset($_SESSION['test'])){
    echo 'Session variable is set ';
    echo $_SESSION['test'];
}else{
    echo 'Session variable is not set';
    $_SESSION['test'] = 'test';
}

when I access the page through HTTPS (ex: https://www.example.com/page.php) It works well, The first time it sets the $_SESSION variable and after that it retrievs it normally.

However, when I access the page through normal HTTP, every time I get "Session variable is not set". When I checked the request/response cookies in both situations, I found that when using HTTPS the PHPSESSID cookie is retrieved from the first request of the page, and from the second request on the same PHPSESSID is sent as request cookie. However, over normal HTTP every time I load the page, it loads without sending any cookies, it gets different PHPSESSID cookie but it doesn't save it and use it for the next requests of the page.

I want to know the causes and solutions for this problem.

Thank you.

1 Answer 1

0

Session cookies may be sets as only secured

Try using session_set_cookie_params to disable only secure session cookies

$oldparams = session_get_cookie_params();
session_set_cookie_params($oldparams["lifetime"], $oldparams["path"], $oldparams["domain"], false, true);
session_start();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.