session in php works only on the same page, but does not work on other pages in the same websitePHP session lost after redirectwhen I send value from a page to another page using session, the following error appearsHow do I expire a PHP session after 30 minutes?PHP session echo not working?Reference — What does this symbol mean in PHP?Check if PHP session has already startedHow does PHP 'foreach' actually work?Reference - What does this error mean in PHP?Why PHP include is working on local server and not on websiteCannot set session variables in a page that is handling session.upload_progress.namePHP - Session variables not carrying over to next page on one device, but does on anotherPHP Session variable is empty after redirecting to another page

Sleeping solo in a double sleeping bag

Is there such a thing as too inconvenient?

Why don't politicians push for fossil fuel reduction by pointing out their scarcity?

What does it mean to have a subnet mask /32?

Shouldn't the "credit score" prevent Americans from going deeper and deeper into personal debt?

Metal that glows when near pieces of itself

Why are delta bots so finicky?

How to "know" if I have a passion?

Should my "average" PC be able to discern the potential of encountering a gelatinous cube from subtle clues?

Was Tuvok bluffing when he said that Voyager's transporters rendered the Kazon weapons useless?

What is the evidence on the danger of feeding whole blueberries and grapes to infants and toddlers?

The teacher logged me in as administrator for doing a short task, is the whole system now compromised?

Chess software to analyze games

How to persuade recruiters to send me the Job Description?

Can you feel passing through the sound barrier in an F-16?

Can we save the word "unique"?

Why does my house heat up, even when it's cool outside?

How to get the pandadocs from an opportunity?

What is "Wayfinder's Guide to Eberron"?

Why we don't have vaccination against all diseases which are caused by microbes?

In an emergency, how do I find and share my position?

Does Swashbuckler's Fancy Footwork apply if the attack was made with Booming Blade?

Why is Boris Johnson visiting only Paris & Berlin if every member of the EU needs to agree on a withdrawal deal?

How to specify and fit a hybrid machine learning - linear model



session in php works only on the same page, but does not work on other pages in the same website


PHP session lost after redirectwhen I send value from a page to another page using session, the following error appearsHow do I expire a PHP session after 30 minutes?PHP session echo not working?Reference — What does this symbol mean in PHP?Check if PHP session has already startedHow does PHP 'foreach' actually work?Reference - What does this error mean in PHP?Why PHP include is working on local server and not on websiteCannot set session variables in a page that is handling session.upload_progress.namePHP - Session variables not carrying over to next page on one device, but does on anotherPHP Session variable is empty after redirecting to another page






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















I have two simple pages(test.php, test2.php).



test.php



<?php
session_start();
$_SESSION['username'] = "wria";
echo '<a href = "test2.php">click to another page</a> <br>';

if($_SESSION['username'])
echo $_SESSION['username'];

?>


test2.php



<?php
session_start();
var_dump($_SESSION);
if($_SESSION['username'])
echo $_SESSION['username'];

?>


in the output,
when I open test.php, after a refresh
enter image description here



but when I go to test2.php, the output appears:



enter image description here



nothing showing.



Note: this file is on real hosting not local.










share|improve this question





















  • 2





    error_reporting(E_ALL); ini_set('display_errors', '1');

    – AbraCadaver
    Mar 27 at 15:22






  • 2





    Are you sure that is all the code in those 2 files?

    – RiggsFolly
    Mar 27 at 15:23











  • yes that is all codes which I have.

    – Wria Mohammed
    Mar 27 at 15:25











  • Check te protocoll HTTP/HTTPS in href="" or domain is with/without www

    – Gabor
    Mar 27 at 15:26











  • Any errors yet?

    – AbraCadaver
    Mar 27 at 15:33

















2















I have two simple pages(test.php, test2.php).



test.php



<?php
session_start();
$_SESSION['username'] = "wria";
echo '<a href = "test2.php">click to another page</a> <br>';

if($_SESSION['username'])
echo $_SESSION['username'];

?>


test2.php



<?php
session_start();
var_dump($_SESSION);
if($_SESSION['username'])
echo $_SESSION['username'];

?>


in the output,
when I open test.php, after a refresh
enter image description here



but when I go to test2.php, the output appears:



enter image description here



nothing showing.



Note: this file is on real hosting not local.










share|improve this question





















  • 2





    error_reporting(E_ALL); ini_set('display_errors', '1');

    – AbraCadaver
    Mar 27 at 15:22






  • 2





    Are you sure that is all the code in those 2 files?

    – RiggsFolly
    Mar 27 at 15:23











  • yes that is all codes which I have.

    – Wria Mohammed
    Mar 27 at 15:25











  • Check te protocoll HTTP/HTTPS in href="" or domain is with/without www

    – Gabor
    Mar 27 at 15:26











  • Any errors yet?

    – AbraCadaver
    Mar 27 at 15:33













2












2








2


1






I have two simple pages(test.php, test2.php).



test.php



<?php
session_start();
$_SESSION['username'] = "wria";
echo '<a href = "test2.php">click to another page</a> <br>';

if($_SESSION['username'])
echo $_SESSION['username'];

?>


test2.php



<?php
session_start();
var_dump($_SESSION);
if($_SESSION['username'])
echo $_SESSION['username'];

?>


in the output,
when I open test.php, after a refresh
enter image description here



but when I go to test2.php, the output appears:



enter image description here



nothing showing.



Note: this file is on real hosting not local.










share|improve this question
















I have two simple pages(test.php, test2.php).



test.php



<?php
session_start();
$_SESSION['username'] = "wria";
echo '<a href = "test2.php">click to another page</a> <br>';

if($_SESSION['username'])
echo $_SESSION['username'];

?>


test2.php



<?php
session_start();
var_dump($_SESSION);
if($_SESSION['username'])
echo $_SESSION['username'];

?>


in the output,
when I open test.php, after a refresh
enter image description here



but when I go to test2.php, the output appears:



enter image description here



nothing showing.



Note: this file is on real hosting not local.







php session






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 27 at 15:22







Wria Mohammed

















asked Mar 27 at 15:17









Wria MohammedWria Mohammed

3484 silver badges13 bronze badges




3484 silver badges13 bronze badges










  • 2





    error_reporting(E_ALL); ini_set('display_errors', '1');

    – AbraCadaver
    Mar 27 at 15:22






  • 2





    Are you sure that is all the code in those 2 files?

    – RiggsFolly
    Mar 27 at 15:23











  • yes that is all codes which I have.

    – Wria Mohammed
    Mar 27 at 15:25











  • Check te protocoll HTTP/HTTPS in href="" or domain is with/without www

    – Gabor
    Mar 27 at 15:26











  • Any errors yet?

    – AbraCadaver
    Mar 27 at 15:33












  • 2





    error_reporting(E_ALL); ini_set('display_errors', '1');

    – AbraCadaver
    Mar 27 at 15:22






  • 2





    Are you sure that is all the code in those 2 files?

    – RiggsFolly
    Mar 27 at 15:23











  • yes that is all codes which I have.

    – Wria Mohammed
    Mar 27 at 15:25











  • Check te protocoll HTTP/HTTPS in href="" or domain is with/without www

    – Gabor
    Mar 27 at 15:26











  • Any errors yet?

    – AbraCadaver
    Mar 27 at 15:33







2




2





error_reporting(E_ALL); ini_set('display_errors', '1');

– AbraCadaver
Mar 27 at 15:22





error_reporting(E_ALL); ini_set('display_errors', '1');

– AbraCadaver
Mar 27 at 15:22




2




2





Are you sure that is all the code in those 2 files?

– RiggsFolly
Mar 27 at 15:23





Are you sure that is all the code in those 2 files?

– RiggsFolly
Mar 27 at 15:23













yes that is all codes which I have.

– Wria Mohammed
Mar 27 at 15:25





yes that is all codes which I have.

– Wria Mohammed
Mar 27 at 15:25













Check te protocoll HTTP/HTTPS in href="" or domain is with/without www

– Gabor
Mar 27 at 15:26





Check te protocoll HTTP/HTTPS in href="" or domain is with/without www

– Gabor
Mar 27 at 15:26













Any errors yet?

– AbraCadaver
Mar 27 at 15:33





Any errors yet?

– AbraCadaver
Mar 27 at 15:33












4 Answers
4






active

oldest

votes


















1














I think the problem comes from your server configuration. Have you tried it in a local environement ? Here's is some steps to help you debug this which comes from PHP session lost after redirect




First, carry out these usual checks:



  1. Make sure session_start(); is called before any sessions are being called. So a safe bet would be to put it at the beginning of your page, immediately after the opening

  2. After the header redirect, end the current script using exit(); (Others have also suggested session_write_close(); and session_regenerate_id(true), you can try those as well, but I'd use exit();)

  3. Make sure cookies are enabled in the browser you are using to test it on.

  4. Ensure register_globals is off, you can check this on the php.ini file and also using phpinfo().

  5. Make sure you didn't delete or empty the session

  6. Make sure the key in your $_SESSION superglobal array is not overwritten anywhere

  7. Make sure you redirect to the same domain. So redirecting from a www.yourdomain.com to yourdomain.com doesn't carry the session forward.

  8. Make sure your file extension is .php (it happens!)



You may also have to configure a session_save_path like it was said in the post i linked.






share|improve this answer




















  • 1





    thank you, i tried.

    – Wria Mohammed
    Mar 29 at 10:57


















1














if you add this code:
error_reporting(E_ALL);
ini_set('display_errors', 1);
to the top for test2.php, you will see your error.
if you see (No such file or directory) it means your saved session is unavailable, you can change it for another place.
or probably you do not have permission for the folder that session will save on it.






share|improve this answer

























  • thank you, your answer is good, I tried. it is correct. thank you again.

    – Wria Mohammed
    Mar 29 at 21:08


















1














your path to save session is unavailable. You have to change it for an available place.
You can change in php.ini






share|improve this answer

























  • thank you. that is the point.

    – Wria Mohammed
    Mar 30 at 22:09






  • 1





    you are most welcome

    – Faxradin majid
    Mar 30 at 22:37


















0














Likely you do not persist a session id in any way and the new session is being opened each time.



Please have a look at the https://www.php.net/manual/en/session.idpassing.php



There are a few ways it can be 'solved', using cookies, GET parameter etc.






share|improve this answer



























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55380712%2fsession-in-php-works-only-on-the-same-page-but-does-not-work-on-other-pages-in%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I think the problem comes from your server configuration. Have you tried it in a local environement ? Here's is some steps to help you debug this which comes from PHP session lost after redirect




    First, carry out these usual checks:



    1. Make sure session_start(); is called before any sessions are being called. So a safe bet would be to put it at the beginning of your page, immediately after the opening

    2. After the header redirect, end the current script using exit(); (Others have also suggested session_write_close(); and session_regenerate_id(true), you can try those as well, but I'd use exit();)

    3. Make sure cookies are enabled in the browser you are using to test it on.

    4. Ensure register_globals is off, you can check this on the php.ini file and also using phpinfo().

    5. Make sure you didn't delete or empty the session

    6. Make sure the key in your $_SESSION superglobal array is not overwritten anywhere

    7. Make sure you redirect to the same domain. So redirecting from a www.yourdomain.com to yourdomain.com doesn't carry the session forward.

    8. Make sure your file extension is .php (it happens!)



    You may also have to configure a session_save_path like it was said in the post i linked.






    share|improve this answer




















    • 1





      thank you, i tried.

      – Wria Mohammed
      Mar 29 at 10:57















    1














    I think the problem comes from your server configuration. Have you tried it in a local environement ? Here's is some steps to help you debug this which comes from PHP session lost after redirect




    First, carry out these usual checks:



    1. Make sure session_start(); is called before any sessions are being called. So a safe bet would be to put it at the beginning of your page, immediately after the opening

    2. After the header redirect, end the current script using exit(); (Others have also suggested session_write_close(); and session_regenerate_id(true), you can try those as well, but I'd use exit();)

    3. Make sure cookies are enabled in the browser you are using to test it on.

    4. Ensure register_globals is off, you can check this on the php.ini file and also using phpinfo().

    5. Make sure you didn't delete or empty the session

    6. Make sure the key in your $_SESSION superglobal array is not overwritten anywhere

    7. Make sure you redirect to the same domain. So redirecting from a www.yourdomain.com to yourdomain.com doesn't carry the session forward.

    8. Make sure your file extension is .php (it happens!)



    You may also have to configure a session_save_path like it was said in the post i linked.






    share|improve this answer




















    • 1





      thank you, i tried.

      – Wria Mohammed
      Mar 29 at 10:57













    1












    1








    1







    I think the problem comes from your server configuration. Have you tried it in a local environement ? Here's is some steps to help you debug this which comes from PHP session lost after redirect




    First, carry out these usual checks:



    1. Make sure session_start(); is called before any sessions are being called. So a safe bet would be to put it at the beginning of your page, immediately after the opening

    2. After the header redirect, end the current script using exit(); (Others have also suggested session_write_close(); and session_regenerate_id(true), you can try those as well, but I'd use exit();)

    3. Make sure cookies are enabled in the browser you are using to test it on.

    4. Ensure register_globals is off, you can check this on the php.ini file and also using phpinfo().

    5. Make sure you didn't delete or empty the session

    6. Make sure the key in your $_SESSION superglobal array is not overwritten anywhere

    7. Make sure you redirect to the same domain. So redirecting from a www.yourdomain.com to yourdomain.com doesn't carry the session forward.

    8. Make sure your file extension is .php (it happens!)



    You may also have to configure a session_save_path like it was said in the post i linked.






    share|improve this answer













    I think the problem comes from your server configuration. Have you tried it in a local environement ? Here's is some steps to help you debug this which comes from PHP session lost after redirect




    First, carry out these usual checks:



    1. Make sure session_start(); is called before any sessions are being called. So a safe bet would be to put it at the beginning of your page, immediately after the opening

    2. After the header redirect, end the current script using exit(); (Others have also suggested session_write_close(); and session_regenerate_id(true), you can try those as well, but I'd use exit();)

    3. Make sure cookies are enabled in the browser you are using to test it on.

    4. Ensure register_globals is off, you can check this on the php.ini file and also using phpinfo().

    5. Make sure you didn't delete or empty the session

    6. Make sure the key in your $_SESSION superglobal array is not overwritten anywhere

    7. Make sure you redirect to the same domain. So redirecting from a www.yourdomain.com to yourdomain.com doesn't carry the session forward.

    8. Make sure your file extension is .php (it happens!)



    You may also have to configure a session_save_path like it was said in the post i linked.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 27 at 15:39









    ThomasThomas

    658 bronze badges




    658 bronze badges










    • 1





      thank you, i tried.

      – Wria Mohammed
      Mar 29 at 10:57












    • 1





      thank you, i tried.

      – Wria Mohammed
      Mar 29 at 10:57







    1




    1





    thank you, i tried.

    – Wria Mohammed
    Mar 29 at 10:57





    thank you, i tried.

    – Wria Mohammed
    Mar 29 at 10:57













    1














    if you add this code:
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    to the top for test2.php, you will see your error.
    if you see (No such file or directory) it means your saved session is unavailable, you can change it for another place.
    or probably you do not have permission for the folder that session will save on it.






    share|improve this answer

























    • thank you, your answer is good, I tried. it is correct. thank you again.

      – Wria Mohammed
      Mar 29 at 21:08















    1














    if you add this code:
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    to the top for test2.php, you will see your error.
    if you see (No such file or directory) it means your saved session is unavailable, you can change it for another place.
    or probably you do not have permission for the folder that session will save on it.






    share|improve this answer

























    • thank you, your answer is good, I tried. it is correct. thank you again.

      – Wria Mohammed
      Mar 29 at 21:08













    1












    1








    1







    if you add this code:
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    to the top for test2.php, you will see your error.
    if you see (No such file or directory) it means your saved session is unavailable, you can change it for another place.
    or probably you do not have permission for the folder that session will save on it.






    share|improve this answer













    if you add this code:
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    to the top for test2.php, you will see your error.
    if you see (No such file or directory) it means your saved session is unavailable, you can change it for another place.
    or probably you do not have permission for the folder that session will save on it.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 29 at 18:07









    DostiDosti

    358 bronze badges




    358 bronze badges















    • thank you, your answer is good, I tried. it is correct. thank you again.

      – Wria Mohammed
      Mar 29 at 21:08

















    • thank you, your answer is good, I tried. it is correct. thank you again.

      – Wria Mohammed
      Mar 29 at 21:08
















    thank you, your answer is good, I tried. it is correct. thank you again.

    – Wria Mohammed
    Mar 29 at 21:08





    thank you, your answer is good, I tried. it is correct. thank you again.

    – Wria Mohammed
    Mar 29 at 21:08











    1














    your path to save session is unavailable. You have to change it for an available place.
    You can change in php.ini






    share|improve this answer

























    • thank you. that is the point.

      – Wria Mohammed
      Mar 30 at 22:09






    • 1





      you are most welcome

      – Faxradin majid
      Mar 30 at 22:37















    1














    your path to save session is unavailable. You have to change it for an available place.
    You can change in php.ini






    share|improve this answer

























    • thank you. that is the point.

      – Wria Mohammed
      Mar 30 at 22:09






    • 1





      you are most welcome

      – Faxradin majid
      Mar 30 at 22:37













    1












    1








    1







    your path to save session is unavailable. You have to change it for an available place.
    You can change in php.ini






    share|improve this answer













    your path to save session is unavailable. You have to change it for an available place.
    You can change in php.ini







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 30 at 21:42









    Faxradin majidFaxradin majid

    393 bronze badges




    393 bronze badges















    • thank you. that is the point.

      – Wria Mohammed
      Mar 30 at 22:09






    • 1





      you are most welcome

      – Faxradin majid
      Mar 30 at 22:37

















    • thank you. that is the point.

      – Wria Mohammed
      Mar 30 at 22:09






    • 1





      you are most welcome

      – Faxradin majid
      Mar 30 at 22:37
















    thank you. that is the point.

    – Wria Mohammed
    Mar 30 at 22:09





    thank you. that is the point.

    – Wria Mohammed
    Mar 30 at 22:09




    1




    1





    you are most welcome

    – Faxradin majid
    Mar 30 at 22:37





    you are most welcome

    – Faxradin majid
    Mar 30 at 22:37











    0














    Likely you do not persist a session id in any way and the new session is being opened each time.



    Please have a look at the https://www.php.net/manual/en/session.idpassing.php



    There are a few ways it can be 'solved', using cookies, GET parameter etc.






    share|improve this answer





























      0














      Likely you do not persist a session id in any way and the new session is being opened each time.



      Please have a look at the https://www.php.net/manual/en/session.idpassing.php



      There are a few ways it can be 'solved', using cookies, GET parameter etc.






      share|improve this answer



























        0












        0








        0







        Likely you do not persist a session id in any way and the new session is being opened each time.



        Please have a look at the https://www.php.net/manual/en/session.idpassing.php



        There are a few ways it can be 'solved', using cookies, GET parameter etc.






        share|improve this answer













        Likely you do not persist a session id in any way and the new session is being opened each time.



        Please have a look at the https://www.php.net/manual/en/session.idpassing.php



        There are a few ways it can be 'solved', using cookies, GET parameter etc.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 27 at 15:24









        matiitmatiit

        6,1235 gold badges32 silver badges60 bronze badges




        6,1235 gold badges32 silver badges60 bronze badges






























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55380712%2fsession-in-php-works-only-on-the-same-page-but-does-not-work-on-other-pages-in%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript