Page 1 of 3 123 LastLast
Results 1 to 10 of 27

Thread: Stored Sessions Data Not Found

  1. #1
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    245
    Thanks
    86
    Thanked 3 Times in 3 Posts

    Question Stored Sessions Data Not Found

    Happy New Years, Folks,

    My first 2014 puzzlement (may they be few): I'm attempting to use sessions. My efforts produce error messages. I am hoping someone here can explain what I am doing wrong.

    The visitor is presented with a menu that consists of pages s/he can read. One of the menu items is q0.php. In q0.php I define $_SESSION["code"]. Then q0.php causes quotations.txt to load:

    Code:
    <?php
    $_SESSION["code"] = "0";
    header( 'Location: page.php?here=quotations' );
    ?>
    Note: I have no session_start(); statements because I set session.auto_start to "1" in my php.ini file.


    Here is quotations.txt (a grossly abbreviated excerpt), where I attempt to use $_SESSION["code"], defined above in q0.php:


    Code:
    <?php
    $lastUpdate = date( "j F Y", filemtime( "pageContent/quotations.txt" ) );
    
    $quotations = array(
    
    "We, as criminal defense lawyers, are forced to deal with <a href='http://en.wikipedia.org/wiki/Torture_memos' target='_blank'>some of the lowest people on earth</a>.  People who have no sense of right and wrong.  People who will lie in court to get what they want.  People who do not care who gets hurt in the process. It is our job, our sworn duty as criminal defense lawyers, to protect our clients from those people.",
    
    "Cynthia Rosenberry",
    
    "For to him that is joined to all the living there is hope: for a living dog is better than a dead lion.
    <br />
    <br />
    For the living know that they shall die: but the dead know not anything, neither have they any more a reward; for the memory of them is forgotten.
    <br />
    <br />
    Also their love, and their hatred, and their envy, is not perished; neither have they any more a portion for ever in any thing that is done under the sun.",
    
    "Ecclesiastes 8:4-6",
    
    );
    
    
    if (	$_SESSION["code"] == "0"
    ||	$_SESSION["code"] == "1") {
    
    		$bigIndex = rand(0, (count($quotations) -1)) & 65534; 	// select entry, force even
    		$_SESSION["quoteIndex"] = $bigIndex;
    }
    
    else {
    	$bigIndex = $_SESSION["quoteIndex"];
    }
    
    
    echo "<br /><br /><div class='bigquote'> {$quotations [$bigIndex]}  <br /><br /><div align='right'>"; // display
    
    $noSource = "&nbsp;";				// To test whether we need, or can skip, Part Two
    
    if ($quotations [$bigIndex + 1] != $noSource) {	// ...then we have an attribution to display for Part Two,
    
    	 echo $quotations [$bigIndex + 1] . "<br /><br />";	// so display it already
    }
    
    
    if (	$_SESSION["code"] == "0"
    ||	$_SESSION["code"] == "2") {
    
    	$universe = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 0";	// establish Part Three (partial) universe
    
    	$characters = explode(" ", $universe);	// make array of it
    
    	shuffle ($characters);			// go to Buffalo
    
    	$characters = array_slice($characters, 20, 3);	// trim.  The "20" is somewhat arbitrary
    
    	$decorations = array(				// "Mommy, see all the pretty decorations."
    		"&spades;",
    		"<span style='color : #ff4040; '>&hearts;</span>",
    		"<span style='color : #ff4040; '>&diams;</span>",
    		"&int;",
    		"&omega;",
    		"&pi;",
    		"&sum;",
    		"&clubs;"
    );
    
    	shuffle ($decorations);				// to Buffalo, too, the decorations
    
    	$decorations = array_slice($decorations, 0, 3);	// pare decorations
    
    	$decorations = array_merge($decorations, $characters);	// meld tree onto decorations
    
    	shuffle ($decorations);			// again to Buffalo we go, to complete Part Three
    
    	$_SESSION["decorations"] = $decorations;
    }
    
    else {
    	$decorations = $_SESSION["decorations"];
    }
    
    
    echo "Your Access Code is " . implode("&nbsp;", $decorations) . ".<br /><font size='-2'>Access code broken?  Click <a href='http://www.MarainLaw.com/quotations'>*** here ***</a> to replace it.</font><br /><br /><br /></div></div>";?>
    Here are the generated error messages (technically, I suppose, "Notices"):



    Notice: Undefined index: code in /var/www/html/pageContent/quotations.txt on line 2956

    Notice: Undefined index: code in /var/www/html/pageContent/quotations.txt on line 2957


    [Echo'd Quotation, followed by Attribution]


    Notice: Undefined index: code in /var/www/html/pageContent/quotations.txt on line 2984

    Notice: Undefined index: code in /var/www/html/pageContent/quotations.txt on line 2985
    Your Access Code is π j G ♦ ♠ z.
    Access code broken? Click *** here *** to replace it.




    quotations.txt is a very large file that, as I indicated above, I've heavily excerpted. A version that works is at http://www.marainlaw.com/page.php?here=quotations.

    A.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    A few things:

    1. Are you positive that the auto start functionality is working? Remove the header redirect and see if you can get any errors. Even if you're positive it's working, debugging to see if there are errors (make sure you have error reporting on) may point out anything you've missed.
    2. It's unclear what "page.php" is or how it relates to "quotation.txt". Please explain your file setup and why you're executing a text file as a php file.
    3. I see you're using other session arrays in your code. Are those working out fine?
    4. One thing you may want to try is dumping the session array and seeing if everything checks out.

    Also, one last thing: reproduce this error. You can't expect to give us a hundredth of what I'd assume to be a huge PHP script and let us diagnose the issue. Reproducing the error with ~10 lines of code (and nothing irrelevant in between) will allow us to see exactly what's going on and how to solve the issue.

  3. The Following User Says Thank You to Nile For This Useful Post:

    marain (01-04-2014)

  4. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Notice: Undefined index: code in /var/www/html/pageContent/quotations.txt on line 2956
    Look at the error message: On line 2956 of your quotations.txt file, you try to use the (I'm assuming $_SESSION) index code. Problem is, that index does not exist.

    At the most basic level, the solution is to check before you try to use a variable/index that may or may not exist:
    PHP Code:
    <?php

    //  No
    print $_SESSION["index"];

    //  Yes
    if( isset( $_SESSION['index'] ) ){
        print 
    $_SESSION['index'];
    }
    Of course, doing this every time you try to use a variable would be completely impractical - you only need to do this if you are not certain that the variable exists. On the other hand, since you're talking about a session index, I'd guess that it was defined on an earlier page view? If so, then you need to check. You can't assume that it exists.

    If the variable/index should exist, then you need to do some troubleshooting to determine why it doesn't. Where do you define this variable? Look at that part of the code and make sure it is working correctly. See if there are any logical mistakes in your code structure (for example, defining a variable inside an if block and then trying to use it in situations where the if was false).

  5. The Following User Says Thank You to traq For This Useful Post:

    marain (01-04-2014)

  6. #4
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    245
    Thanks
    86
    Thanked 3 Times in 3 Posts

    Default

    Quote Originally Posted by Nile View Post
    A few things:

    1. Are you positive that the auto start functionality is working? Remove the header redirect and see if you can get any errors. Even if you're positive it's working, debugging to see if there are errors (make sure you have error reporting on) may point out anything you've missed.
    2. It's unclear what "page.php" is or how it relates to "quotation.txt". Please explain your file setup and why you're executing a text file as a php file.
    3. I see you're using other session arrays in your code. Are those working out fine?
    4. One thing you may want to try is dumping the session array and seeing if everything checks out.

    Also, one last thing: reproduce this error. You can't expect to give us a hundredth of what I'd assume to be a huge PHP script and let us diagnose the issue. Reproducing the error with ~10 lines of code (and nothing irrelevant in between) will allow us to see exactly what's going on and how to solve the issue.
    1. I'm not sure what you're suggesting here. The heading redirect is where I (attempt to) establish $_SESSION["code"].

    2. Well that's a really good question. By way of background, I've been maintaining this site for several years, but I did not originally create it. Here is the code for page.php:

    Code:
    <?php
    // session_start should be the first line in the file
    //session_start();
    
    date_default_timezone_set( 'America/New_York');
    
    // save the get variable (?here=filename) which defines the text content to show
    $here = $_GET['here'];
    
    // default to home page if this file has been invoked without a get variable
    if ( ! isset( $_GET['here'] ) ) $here = 'index';
    // you could instead force the error page in this case (although that would be illogical), like this:
    // if ( ! isset( $_GET['here'] ) ) header( 'Location: 404.html' );
    
    // show the error page if this file has been invoked with an invalid get variable
    elseif ( ! is_file( 'pageContent/' . $_GET['here'] . '.txt' ) ) header( 'Location: 404.html' );
    // you could instead force the home page in this case, like this:
    // if ( ! is_file( 'pageContent/' . $_GET['here'] . '.txt' ) ) $here = 'index';
    
    // we can load this now that we are sure that we have a valid get variable
    require 'includes/errorCheck.inc.php';
    
    require 'includes/page.class.php';
    $page = new page;
    $page -> here = $here;
    $page -> showPage();
    Bottom line: I lack the expertise to explain (or understand) how it functions.

    3. I use session arrays in four places. All give the error message.

    4. I either don't have the tools, or don't know how to do that.

    I've provided the entire script, more or less. What "more or less" means is that $quotations = array( contains several hundred entries. For simplification purposes, I provided only a few. So the only material excised is additional entries in the $quotations = array(.

  7. #5
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    245
    Thanks
    86
    Thanked 3 Times in 3 Posts

    Default

    Traq,

    I'll see if I can set up a test file to make those inquiries.

    A.

  8. #6
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    245
    Thanks
    86
    Thanked 3 Times in 3 Posts

    Default

    I seem to be digging a bigger and bigger hole for myself: I made a test file, and eliminated all but a few entries in the quotations array. I then tried to incorporate Traq's diagnostics suggestion, i.e.
    Code:
    if ( isset( $_SESSION['code'] ) ){
        print $_SESSION['code'];
    }
    
    else {
    	print "$_SESSION['code'] not found";
    }
    and get parse error. I comment out that section and get numerous additional error messages.

    Here is the test file (with diagnostics commented out), in its entirety:
    Code:
    <?php
    
    $quotations = array(
    
    "We, as criminal defense lawyers, are forced to deal with <a 
    
    href='http://en.wikipedia.org/wiki/Torture_memos' target='_blank'>some of the lowest people on 
    
    earth</a>.  People who have no sense of right and wrong.  People who will lie in court to get what 
    
    they want.  People who do not care who gets hurt in the process. It is our job, our sworn duty as 
    
    criminal defense lawyers, to protect our clients from those people.",
    
    "Cynthia Rosenberry",
    
    "An advocate by the sacred duty which he owes his client knows, in the discharge of that office, but 
    
    one person in the world--that client and none other.",
    
    "Henry Lord Brougham",
    
    "It is wrong to expect a reward for your struggles. The reward is the act of struggle itself, not 
    
    what you win.&nbsp; Even though you can&#39;t expect to defeat the absurdity of the world, you must 
    
    make that attempt.&nbsp;That&#39;s morality.&nbsp; That&#39;s religion. That&#39;s art.&nbsp; 
    
    That&#39;s life.",
    
    "Phil Ochs",
    
    
    "For to him that is joined to all the living there is hope: for a living dog is better than a dead 
    
    lion.
    <br />
    <br />
    For the living know that they shall die: but the dead know not anything, neither have they any more 
    
    a reward; for the memory of them is forgotten.
    <br />
    <br />
    Also their love, and their hatred, and their envy, is not perished; neither have they any more a 
    
    portion for ever in any thing that is done under the sun.",
    
    "Ecclesiastes 8:4-6",
    
    );
    
    /***********************************************************************
    
    if ( isset( $_SESSION['code'] ) ){
        print $_SESSION['code'];
    }
    
    else {
    	print "$_SESSION['code'] not found";
    }
    
    ***********************************************************************/
    
    
    if (	$_SESSION["code"] == "0"
    ||	$_SESSION["code"] == "1") {
    
    		$bigIndex = rand(0, (count($quotations) -1)) & 65534; 	// select entry, force even
    		$_SESSION["quoteIndex"] = $bigIndex;
    }
    
    else {
    	$bigIndex = $_SESSION["quoteIndex"];
    }
    
    
    echo "<br /><br /><div class='bigquote'> {$quotations [$bigIndex]}  <br /><br /><div 
    
    align='right'>"; // display
    
    $noSource = "&nbsp;";				// To test whether we need, or can skip, Part Two
    
    if ($quotations [$bigIndex + 1] != $noSource) {	// ...then we have an attribution to display for 
    
    Part Two,
    
    	 echo $quotations [$bigIndex + 1] . "<br /><br />";	// so display it already
    }
    
    if (	$_SESSION["code"] == "0"
    ||	$_SESSION["code"] == "2") {
    
    	$universe = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n 
    
    o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 0";	// establish Part Three (partial) universe
    
    	$characters = explode(" ", $universe);	// make array of it
    
    	shuffle ($characters);			// go to Buffalo
    
    	$characters = array_slice($characters, 20, 3);	// trim.  The "20" is somewhat arbitrary
    
    	$decorations = array(				// "Mommy, see all the pretty decorations."
    		"&spades;",
    		"<span style='color : #ff4040; '>&hearts;</span>",
    		"<span style='color : #ff4040; '>&diams;</span>",
    		"&int;",
    		"&omega;",
    		"&pi;",
    		"&sum;",
    		"&clubs;"
    );
    
    	shuffle ($decorations);				// to Buffalo, too, the decorations
    
    	$decorations = array_slice($decorations, 0, 3);	// pare decorations
    
    	$decorations = array_merge($decorations, $characters);	// meld tree onto decorations
    
    	shuffle ($decorations);			// again to Buffalo we go, to complete Part Three
    
    	$_SESSION["decorations"] = $decorations;
    }
    
    else {
    	$decorations = $_SESSION["decorations"];
    }
    
    
    echo "Your Access Code is " . implode("&nbsp;", $decorations) . ".<br /><font size='-2'>Access code 
    
    broken?  Click <a href='http://www.MarainLaw.com/test'>*** here ***</a> to replace it.</font><br 
    
    /><br /><br /></div></div>";?>
    ALSO, to address the issue of whether the php.ini parameter was set correctly, I inserted session_start(); into the calling module, t0.pho. Here is the t0.php code:
    Code:
    <?php
    session_start();
    $_SESSION["code"] = "0";
    header( 'Location: page.php?here=test' );
    ?>
    The error messages now are much increased:

    Notice: Undefined index: code in /var/www/html/pageContent/test.txt on line 46

    Notice: Undefined index: code in /var/www/html/pageContent/test.txt on line 47

    Notice: Undefined index: quoteIndex in /var/www/html/pageContent/test.txt on line 54

    Notice: Undefined index: in /var/www/html/pageContent/test.txt on line 58






    Cynthia Rosenberry


    Notice: Undefined index: code in /var/www/html/pageContent/test.txt on line 67

    Notice: Undefined index: code in /var/www/html/pageContent/test.txt on line 68

    Notice: Undefined index: decorations in /var/www/html/pageContent/test.txt on line 101

    Warning: implode() [function.implode]: Invalid arguments passed in /var/www/html/pageContent/test.txt on line 105
    Your Access Code is .
    Access code broken? Click *** here *** to replace it.

    The test page is http://www.marainlaw.com/page.php?here=test .

  9. #7
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    All I can suggest is that a) your session indexes are not being set as you expect, or b) your sessions are not being carried over as you expect. Have you checked your cookies to see if you have the session cookie for this domain (unless you've changed it, the cookie name will probably be "PHPSESSID")?

    After setting your session variables, try echo'ing them back to see if they were set successfully and that they contain the value(s) you expect.

    On your quotations page, you can do print_r( $_SESSION ) at the beginning of the file to see what data the session contains.

    You can use session_id to check if your sessions are, indeed, being started automatically (you will get a value if a session exists, or an empty string if not).

  10. #8
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    The thing is we really can't help you until we know what the issue is, and it's rather hard to identify the issue when we only have a sliver of code. However, we're also not going to look through 3k+ lines of code just to decipher the issue. In this situation, there's really only a few options. You can either debug and try to find out what's not working with the sessions, or, again, reproduce the error in 10 lines or less, upload the code, and we can take a look to see what the issue is.

    One thing that could be the issue is the session is expiring right after it's being set. To know if this is a case, create a test page and paste the following into it, then execute:
    PHP Code:
    <?php
    echo 'session_cache_expire(): '.session_cache_expire().'<br>';
    echo 
    'session_cache_limiter(): '.session_cache_limiter();
    Give us the output. This will let us look deeper into the php ini session settings and see if anything is messed up in there. An ideal output for this should be what you see on this example page I've set up.

    On thing you haven't told us is whether or not sessions usually work on your server. To test that, run something like the following. Once the page loads, reload it again.
    PHP Code:
    <?php
    session_start
    (); //just for precaution

    if( isset( $_SESSION'visited' ] ))
    {
        echo 
    "Sessions appear to be working!";
        
    session_unset();
        exit;
    }

    $_SESSION'visited' ] = true;
    echo 
    "Session set attempted. Refresh.";
    You can also see a sample execution here. As you refresh, the page should alternate between messages (the session unsets when it is detected, then you refresh and the if returns false, so it sets the session again... and so on).

    Running both of these should give us a little bit of insight. However, nothing would be better then reproducing the error and sending us the minimal code. @traq also had a good idea to use session_id() and see if the session even has an ID. If nothing works, one more thing you can try is disabling cookies and give the second script above one more try. Sessions work without cookies, so if the session is appended to the URL properly, something is wrong when the session is being set within a cookie.

    Also, when I said to dump the session information, I essentially meant to use a function like var_dump() or print_r() (as @traq requested above).
    Last edited by Nile; 01-04-2014 at 09:15 PM.

  11. #9
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    245
    Thanks
    86
    Thanked 3 Times in 3 Posts

    Default

    Quote Originally Posted by traq View Post
    All I can suggest is that a) your session indexes are not being set as you expect, or b) your sessions are not being carried over as you expect. Have you checked your cookies to see if you have the session cookie for this domain (unless you've changed it, the cookie name will probably be "PHPSESSID")?

    After setting your session variables, try echo'ing them back to see if they were set successfully and that they contain the value(s) you expect.

    On your quotations page, you can do print_r( $_SESSION ) at the beginning of the file to see what data the session contains.

    You can use session_id to check if your sessions are, indeed, being started automatically (you will get a value if a session exists, or an empty string if not).
    Traq,

    Your suspicion that either session indices are not being set, or that sessions are not being carried over, is entirely correct. That tends to beg the question, of course, as to "why?"

    Checking, I found PHPSESSID=78e370fb4564290a1f8b88cdd9969004. The significance of that is beyond my pay grade ;-)

    I sense that session_id would tell me nothing since, to deal with that possibility, I inserted a session_start(). The only thing that that changes is that I get the additional (expected) message, "Notice: A session had already been started - ignoring session_start() in /var/www/html/pageContent/test.txt on line 2."

  12. #10
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Working backwards through your post here.
    Quote Originally Posted by marain View Post
    Checking, I found PHPSESSID=78e370fb4564290a1f8b88cdd9969004. The significance of that is beyond my pay grade ;-)
    The significance of the ID is that there is indeed a session being initiated. If there were no session you'd likely get an empty string. As for using session_start(), you wouldn't need to put that at the top of your code if you're automatically starting sessions, so don't worry about that. You may want to do a session_regenerate_id() just to make sure nothing is caching/messing up anywhere.

    Quote Originally Posted by marain View Post
    Your suspicion that either session indices are not being set, or that sessions are not being carried over, is entirely correct. That tends to beg the question, of course, as to "why?"
    Since it does seem that the session is working properly, the issue may be that the session isn't lasting long enough or isn't being properly set. See my above post and follow the instructions so we can further diagnose what's going on here.

Similar Threads

  1. Refill "File" form field with stored image data
    By pelaej in forum MySQL and other databases
    Replies: 3
    Last Post: 10-22-2010, 07:28 PM
  2. sql stored procedure
    By aldo007 in forum MySQL and other databases
    Replies: 0
    Last Post: 08-30-2010, 01:41 PM
  3. Types of data to be stored in an array
    By heavensgate15 in forum PHP
    Replies: 1
    Last Post: 05-14-2010, 05:10 PM
  4. Increment a Value Stored in the DB
    By Moshambi in forum MySQL and other databases
    Replies: 3
    Last Post: 04-08-2009, 11:21 AM
  5. login script "user data not found"
    By jimo in forum PHP
    Replies: 10
    Last Post: 01-13-2007, 07:10 PM

Bookmarks

Posting Permissions

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