Web Analytics Made Easy -
StatCounter can't retrieve session variable from another page - CodingForum

Announcement

Collapse
No announcement yet.

can't retrieve session variable from another page

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • can't retrieve session variable from another page

    Hi I can't see why I don't get the session variable from another page. It should be easy.

    Page 1
    echo $_SESSION["current_user_name"];
    $_SESSION["name"] = $_SESSION["current_user_name"]

    This will write "John"

    Page 2
    $current_user_name = $_SESSION["current_user_name"];
    echo $current_user_name;
    $name = $_SESSION["current_user"];
    echo $name;


    This writes out nothing.

    I also tested to pass the session to Page 2, no result:

    <a href="test5.php" Onclick="<?php $_SESSION["current_user_name"]="John"; ?>">Start</a>

    I use session_start(); at start on all pages.

    What is wrong? Thanks

  • #2
    You have three different variations of the same(?) session parameter: current_user, current_user_name and name. It might be a good idea to boilt that down to just one.

    If you're still having trouble, do some basic debugging: var_dump() the session content, check the session cookie in your browser, see if the session files are written correctly.
    Last edited by Jacques1; Sep 7, 2016, 05:49 PM.

    Comment


    • #3
      var_dump() gives NULL.
      What can I do more?

      if $_SESSION["current_user_name"] has a value on Page 1. Shouldnt it be to just declare it as $current_user_name = $_SESSION["current_user_name"]; to retrieve it on page2?

      Comment


      • #4
        Page 1:
        PHP Code:
        <?php

        session_start
        ();

        $_SESSION['foo'] = 'bar';

        var_dump(session_id());


        Page 2:
        PHP Code:
        <?php

        session_start
        ();

        var_dump(session_id());
        var_dump($_SESSION);


        What does each script say?

        Comment


        • #5
          This will give:

          Page1:

          Code:
          $_SESSION['foo'] = 'bar'; 
          echo $_SESSION['foo'];
          var_dump(session_id());
          barstring(0) ""

          Page 2

          Code:
          echo $_SESSION['foo'];
          	var_dump(session_id()); 
          var_dump($_SESSION);
          string(32) "b28260670ec7c3b13bf42de545c47a73" array(0) { }

          Comment


          • #6
            That is not the code I gave you. It's actually completely worthless, because you've obviously forgotten the session_start() in the first script.

            Use – my – code. If you want anybody to help you, you need to stop interfering with our debugging attempts.

            Comment


            • #7
              Sorry stupid of me.

              Your code will give:

              Page 1

              string(0) ""

              Page 2

              string(32) "b28260670ec7c3b13bf42de545c47a73" array(0) { }

              Just another question, is it possible to edit my own post?

              Comment


              • #8
                Originally posted by md7dani View Post
                Your code will give:

                Page 1

                string(0) ""
                Are you absolutely, positively sure that you have a session_start() call on top of the script?



                Originally posted by md7dani View Post
                Just another question, is it possible to edit my own post?
                No. Only after you've written a certain number of posts.

                Comment


                • #9
                  Yes I have session_start() in the header on all pages.

                  Comment


                  • #10
                    Then you're either looking at the wrong code, or there's something horribly wrong with your PHP installation.

                    Is this happening on your PC? Which operating system? Which PHP version? What does the “session” section of phpinfo() say?

                    Comment


                    • #11
                      Im running it on a web server online with PHP Version 5.6.25. Im using Win 7, and Chrome. Just wondering why the session variable is correctly sent and retrieved on Page 1 but not on Page2 (Start). I should say that Im sending the session from Page2 (with login and header) to page1 where it is retrieved correctly, but not back to Page2 (start) or other pages. I will test the session script with less code and pages as possible and see what happens. Maybe I will understand more.

                      Session sections:

                      Session Support enabled
                      Registered save handlers files user
                      Registered serializer handlers php_serialize php php_binary wddx
                      Directive Local Value Master Value
                      session.auto_start Off Off
                      session.cache_expire 180 180
                      session.cache_limiter nocache nocache
                      session.cookie_domain no value no value
                      session.cookie_httponly Off Off
                      session.cookie_lifetime 0 0
                      session.cookie_path / /
                      session.cookie_secure Off Off
                      session.entropy_file no value no value
                      session.entropy_length 0 0
                      session.gc_divisor 100 100
                      session.gc_maxlifetime 1440 1440
                      session.gc_probability 1 1
                      session.hash_bits_per_character 4 4
                      session.hash_function 0 0
                      session.name PHPSESSID PHPSESSID
                      session.referer_check no value no value
                      session.save_handler files files
                      session.save_path /tmp /tmp
                      session.serialize_handler php php
                      session.upload_progress.cleanup On On
                      session.upload_progress.enabled On On
                      session.upload_progress.freq 1% 1%
                      session.upload_progress.min_freq 1 1
                      session.upload_progress.name PHP_SESSION_UPLOAD_PROGRESS PHP_SESSION_UPLOAD_PROGRESS
                      session.upload_progress.prefix upload_progress_ upload_progress_
                      session.use_cookies On On
                      session.use_only_cookies On On
                      session.use_strict_mode Off Off
                      session.use_trans_sid 0 0

                      Just want to thank you for your time helping.

                      Comment


                      • #12
                        Don't test your stuff on a live server. Set up a local development environment and use that for testing. There are packages like XAMPP which can be installed with a few clicks and contain all necessary software (Apache, PHP, MySQL, phpmyadmin, ...).

                        Comment


                        • #13
                          Originally posted by md7dani View Post
                          Yes I have session_start() in the header on all pages.

                          It sounds like you are doing more than running @Jaques1 code by itself. DO NOT do anything other than EXACTLY what @Jaques1 gave you for each file. He did not give you any "headers". DO NOT integrate it with anything you already have. Run his code ALL BY ITSELF.
                          To save time, lets just assume I am almost never wrong.

                          The XY Problem
                          The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

                          Make A Donation https://www.paypal.me/KevinRubio

                          Comment

                          Working...
                          X