Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Posts
    478
    Thanks
    7
    Thanked 3 Times in 3 Posts

    problem with array

    I have been having an issue with the following script and can not figure out what is wrong with it.

    Code:
    foreach($_SESSION as $keyA => $varA) {
    	if (is_array($keyA)) {
    		foreach($keyA as $keyB => $varB) {
    		$sessionsA .= 'session_array: [' . $keyB . '] ' . $varB . ' | ';
    		}
    	} else {
    		$sessionsA .= 'sessions: [' . $keyA . '] ' . $varA . ' | ';
    	}
    }
    What I am wanting is to grab all the sessions that exists for the visitor and have them stored in a single variable. If the session is an array then I wish for them to be stored also but shown that it is an array by using session_array instead of just 'sessions'

    I do not get any error, the page just never completes.

    Am I doing this right for starters ? if not how do I correctly get all sessions variables for that visitor ?

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,691
    Thanks
    25
    Thanked 656 Times in 655 Posts
    $_SESSION is an array to begin with. So you don't need to do thing to make it one.
    You know what is in the array cause you program that.
    And you should/must start your PHP with session_start();

    Maybe this will help:
    PHP Code:
     <?php
    session_start
    ();
    /*set up the info in $_SESSION*/
    $_SESSION['user_name'] = 'needsomehelp';
    $_SESSION['user_id'] = 'Green Hornet';
    $_SESSION['favorite_color'] = 'steel gray';
    $_SESSION['test'] = 42;
    $_SESSION['bling'] = 38;
    $sessionsA '';

    /*show with foreach*/
    foreach($_SESSION as $keyA => $varA) {
        
    $sessionsA .= "$keyA:[$varA]<br>";
    }
    echo 
    $sessionsA;

    /*show with array dump*/
    echo "Or ----------------------------<br>";
    print_r($_SESSION);
    ?>
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 Posts
    This line:

    if (is_array($keyA)) {

    $keyA will be an index key / value NEVER an array. You should be looking at the $varA I suspect instead.
    I can't really think of anything to write here now...

  • #4
    Regular Coder
    Join Date
    Oct 2009
    Posts
    478
    Thanks
    7
    Thanked 3 Times in 3 Posts
    Thank you both for your replies.

    Yes all scripts start with session_start(); and I shall try your suggestion tangoforce, just in case.

    I could be wrong, but after hours of testing with google crawler page and viewing the page myself I think I have found a few bugs in my own code. One being that I was not closing the mysqli connection, and think that google being connected for so long caused the server to overload and give the error500 page to its servers.

    Also I have a error grabbing script at the end of all scripts in my footer function and this gets the last_error() and stores this in one of my mysql tables. But I found that I had already got the last error in a previous script that I forgot about and this caused multiple problems and no error to help me work out where the problem was.

    Seems like google is crawling my site ok for now, I shall monitor it for a few days and see how it goes.

    Still no logging of the sessions yet as this has helped in the past with debugging. But I only turn it on when someone reports a problem.

    Would the code I have above be right to get all sessions be it an array or otherwise and have this stored in a single variable ?

    Or is there a better/right way ? if so how.

    Thanks

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 Posts
    Quote Originally Posted by needsomehelp View Post
    and I shall try your suggestion tangoforce, just in case.
    Just in case? I am telling you a fundamental basic of php arrays. The index is either a number or associative. It can not be an array so it is pointless you testing for it.

    Do this as you clearly don't believe me in your over confidence of your skills:

    var_dump($_SESSION);

    You will see that all of your array indexes are either numbers or strings.

    Quote Originally Posted by needsomehelp View Post
    One being that I was not closing the mysqli connection
    It shouldn't make any difference because as soon as the php scripts execution is finished, php automatically closes any open database connections. I never bother with mysqli_close() in any ofmy scripts.

    It's more likely that you have a loop that is never terminating somewhere and mixed with set_time_limit() either via php.ini or your script and it could run forever.

    Quote Originally Posted by needsomehelp View Post
    Would the code I have above be right to get all sessions be it an array or otherwise and have this stored in a single variable ?

    Or is there a better/right way ? if so how.
    As I said to you earlier, look at the correct items in your loops and arrays for a start. Your current code does not do this.
    I can't really think of anything to write here now...

  • #6
    Regular Coder
    Join Date
    Oct 2009
    Posts
    478
    Thanks
    7
    Thanked 3 Times in 3 Posts
    I shall look more in to the array in the sessions, as i have one multidimensional array in one of my sessions.

    As for closing the connection... I done this and now i have yet to see an error via google crawling the page.

    There are no loops anywhere apart from the code i gave before, so can not see it being an infinite loop issue.

  • #7
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 Posts
    Quote Originally Posted by needsomehelp View Post
    There are no loops anywhere apart from the code i gave before, so can not see it being an infinite loop issue.
    foreach($_SESSION as $keyA => $varA) {

    Oh yes there is..
    I can't really think of anything to write here now...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •