PHPnewbie2 Posted January 8, 2007 Share Posted January 8, 2007 Hallo all,Like my username says, I'm still a newbie to PHP and I'm having an issue adapting some (simple?) code from a book to test whether cookies are active or not:[b]SessionTest.php[/b](procedural version; seems to work fine... though if I'm making dreadful mistakes please let me know!)[code]<?phpsession_name('SESSIONTEST');session_start();// check if cookies are active by comparing previously stored $randnum value to that of session $fromsession = $_SESSION['randnum'];$fromcookie = $_COOKIE['randnum'];if ( $_SESSION['randnum'] == $_COOKIE['randnum'] ) { $cookiecheck = true; }else { $cookiecheck = false; }// update or create $randnum session and cookie values (before echoing any output)$randnum = rand(0,99999);$_SESSION['randnum'] = $randnum;setcookie( 'randnum', $randnum, time()+60*60*24*30, '/' );// check if cookies are active by comparing $randnum valuesif ( $cookiecheck == true ) { echo '$_SESSION[' . $fromsession . '] == $_COOKIE[' . $fromcookie . ']<br />'; echo 'Cookies are active!!! :)<br /><br />'; }else { echo '$_SESSION[' . $fromsession . '] != $_COOKIE[' . $fromcookie . ']<br />'; echo 'Cookies are *not* active!!! :(<br /><br />'; }// increment or create counter session varif ( isset($_SESSION['counter']) ) { $_SESSION['counter'] ++; }else { $_SESSION['counter'] = 1; }// display session infoprint_r( session_name() ); echo "<br />\n";print_r( $_SESSION ); echo "<br />\n";print_r( session_id() ); echo "<br />\n";?><p><a href='SessionTest.php'>Back to SessionTest.php</a></p>[/code]After getting the above code to (finally) work, I took the next step and tried making an object-oriented class version out of it:[b]SessionTest2.php[/b](see the UpdateRandNum function/method for the portion of the code that doesn't seem to be working now)[code]<?phpclass CheckCookies { var $fromsession; var $fromcookie; var $cookiesactive; var $randnum; function CheckCookies() // initiate session // check if cookies are active by comparing previously stored $randnum values { session_name('SESSIONTEST2'); session_start(); $fromsession = $_SESSION['randnum']; $fromcookie = $_COOKIE['randnum']; if ( $fromsession == $fromcookie ) { $cookiesactive = true; } else { $cookiesactive = false; } $this->UpdateRandNum(); $this->UpdateCounter(); $this->DisplayCookieStatus(); } function UpdateRandNum() // generate new $randnum then update session and cookie values // *** THIS PART DOES NOT WORK!!! *** { $randnum = rand(0,99999); $_SESSION['randnum'] = $randnum; setcookie( 'randnum', $randnum, time()+60*60*24*30, '/' ); } function UpdateCounter() // increment or create counter session var { if ( isset($_SESSION['counter']) ) { $_SESSION['counter'] ++; } else { $_SESSION['counter'] = 1; } } function DisplayCookieStatus() // display cookie status results { if ( $cookiesactive == true ) { echo '$_SESSION[' . $fromsession . '] == $_COOKIE[' . $fromcookie . ']<br />'; echo 'Cookies are active!!! :)<br /><br />'; } else { echo '$_SESSION[' . $fromsession . '] != $_COOKIE[' . $fromcookie . ']<br />'; echo 'Cookies are *not* active!!! :(<br /><br />'; } // display more session info print_r( session_name() ); echo "<br />\n"; print_r( $_SESSION ); echo "<br />\n"; print_r( session_id() ); echo "<br />\n"; } }$cookiecheck = new CheckCookies();?><p><a href='SessionTest2.php'>Back to SessionTest2.php</a></p>[/code]So, since the other portions of the [b]SessionTest2.php[/b] code appear to be functional it looks (to me, anyway) like the only problem is that the: $_SESSION['randnum'] = $randnum; setcookie( 'randnum', $randnum, time()+60*60*24*30, '/' );lines aren't being executed properly from inside the class -- which is weird since session_start(), session_name(), $randnum, and the counter still operate.I'm [i]guessing[/i] it's a variable scope issue, but have no clue how to fix it. <:/ Perhaps there's some sort of pass-by-reference workaround with the session variable (experimenting with 'global' didn't seem to help), but what about the setcookie function? Hopefully I'm just missing something that's obvious to the rest of you...Also, while I'm wondering about it, are the: var $fromsession; var $fromcookie; var $cookiesactive; var $randnum;lines really necessary or are all vars created within a class freely passed around between all the functions/methods it contains when running PHP 4.4.4?Thanks for any and all assistance! Link to comment https://forums.phpfreaks.com/topic/33295-updating-session-cookie-values-from-within-a-class-in-php-444/ Share on other sites More sharing options...
fert Posted January 8, 2007 Share Posted January 8, 2007 you don't need session_name() in your code and you need to have session_start(); at the very top of your pages Link to comment https://forums.phpfreaks.com/topic/33295-updating-session-cookie-values-from-within-a-class-in-php-444/#findComment-155563 Share on other sites More sharing options...
PHPnewbie2 Posted January 8, 2007 Author Share Posted January 8, 2007 Thanks for responding. I tried commenting out the session_name() line and moving the session_start() line out of the class directly beneath the <?php line as you suggested but still had the same problem...[quote author=fert link=topic=121463.msg499520#msg499520 date=1168231319]you don't need session_name() in your code[/quote]I ran both the procedural and class versions from the same directory to test them; the session_name() lines were included so the counters could be updated independently rather than both sharing the value of the default PHPSESSID session name.[quote author=fert link=topic=121463.msg499520#msg499520 date=1168231319]you need to have session_start(); at the very top of your pages[/quote]My understanding is that you're only required to execute all the session/cookie/header stuff before any text is echoed/printed to avoid the "headers already sent" error message. There's no such error when I ran the above [b]SessionTest2.php[/b] code though since the DisplayCookieStatus() method is called afterwards....Anyway, I messed around with the coding a while longer and found I needed to do one of two [i][b]very simple[/b][/i] things:[1] -- consistently put "$this->" in front of all variables I planned to reuse in the display method [i]>newbie sigh[/i]<[2] -- call/define the display method like so -- DisplayCookieStatus($cookiesactive, $fromsession, $fromcookie) -- to pass the vars I wanted to reuse as in the (final?) version below.[b]SessionTest3.php[/b][code]<?phpclass CheckCookies { function CheckCookies() // initiate session // check if cookies are active by comparing previously stored $randnum values { session_name('SESSIONTEST3'); session_start(); $fromsession = $_SESSION['randnum']; $fromcookie = $_COOKIE['randnum']; if ( $fromsession == $fromcookie ) { $cookiesactive = true; } else { $cookiesactive = false; } $this->UpdateRandNum(); $this->UpdateCounter(); $this->DisplayCookieStatus($cookiesactive, $fromsession, $fromcookie); // wasn't passing values from CheckCookies() constructor to DisplayCookieStatus() before } function UpdateRandNum() // generate new $randnum then update session and cookie values { $randnum = rand(0,99999); $_SESSION['randnum'] = $randnum; setcookie( 'randnum', $randnum, time()+60*60*24*30, '/' ); } function UpdateCounter() // increment or create counter session var { if ( isset($_SESSION['counter']) ) { $_SESSION['counter'] ++; } else { $_SESSION['counter'] = 1; } } function DisplayCookieStatus($cookiesactive, $fromsession, $fromcookie) // display cookie status results { if ( $cookiesactive == true ) { echo '$_SESSION[' . $fromsession . '] == $_COOKIE[' . $fromcookie . ']<br />'; echo 'Cookies are active!!! :)<br /><br />'; } else { echo '$_SESSION[' . $fromsession . '] != $_COOKIE[' . $fromcookie . ']<br />'; echo 'Cookies are *not* active!!! :(<br /><br />'; } // display more session info print_r( session_name() ); echo "<br />\n"; print_r( $_SESSION ); echo "<br />\n"; print_r( session_id() ); echo "<br />\n"; } }$cookiecheck = new CheckCookies();?><p><a href='SessionTest3.php'>Back to SessionTest3.php</a></p>[/code]So, I mistakenly thought the UpdateRandNum() method's statements were the problem when it was really the DisplayCookieStatus() method's not receiving the vars it needed. [i]>newbie sigh #2<[/i]If you notice any way(s) to further improve on the [b]SessionTest3.php[/b] version please let me know! :D-------BTW, in PHP4 is initializing vars ala: var $varname;important to do? It doesn't seem to be necessary for scripts to run... ??? Link to comment https://forums.phpfreaks.com/topic/33295-updating-session-cookie-values-from-within-a-class-in-php-444/#findComment-156025 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.