Re: php 5.2.x - Fatal error: Cannot re-assign $this in ...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



>>>>> "Shawn" == Shawn McKenzie <nospam@xxxxxxxxxxxxx> writes:

Shawn> Shawn McKenzie wrote:
>> John Stoffel wrote:
>>> Hi,
>>> 
>>> I'm looking for some help with PHP5.2.x hacking on a web site to
>>> cleanup how the user submitted search form is handled.  Basically I
>>> want to make it simple for the end user to a) refine their query, b)
>>> not have to answer popups if they hit the reload button, and c) so I
>>> can learn more PHP.  I found this nice reference:
>>> 
>>> http://us2.php.net/manual/en/language.variables.external.php
>>> 
>>> which has two sections by "wayne" and "Jonas Lindel" talking about how
>>> you can take the results of a PHP POST request, stuff the vars into
>>> your session, and redirect back to the same page without the POST vars
>>> set, then complete the query.
>>> 
>>> The idea is so that when people hit the "back" or "reload" buttons,
>>> they don't keep getting asked whether they want to resubmit their
>>> data.
>>> 
>>> What I'm trying is not strictly needed, but it would be a nice cleanup
>>> of the interface of the search forms for this site.  
>>> 
>>> I've been googling, reading php5.x docs and trying to understand what
>>> needs to be done here.  I think I'm just missing how to re-assign
>>> $this properly, so that when the redirect happens, the proper env is set.
>>> 
>>> Here's the snippet I'm trying to use, which bombs out with:
>>> 
>>> Fatal error: Cannot re-assign $this in .... on line 45
>>> 
>>> And the code I'm using is:
>>> 
>>> <?
>>> 
>>> // We want to put the search query into a session so we can restore it
>>> // easily when users goto look at full_holdings.php and then return to
>>> // the overall display page.  We'll need to do a session_destroy in
>>> // index.php I think.  See the tutorial(s) at
>>> // http://www.phpf1.com/tutorial/php-sessions.html
>>> // http://us3.php.net/manual/en/book.session.php
>>> 
>>> session_start();
>>> 
>>> // Now see Jonas Lindel's & Wayne's code at
>>> // http://us2.php.net/manual/en/language.variables.external.php for
>>> // this *nice* trick.
>>> 
>>> //is there POST or GET data we should look at
>>> if($_POST or ($_GET and (!isset($_SESSION['skipnextget'])))) {
>>> 
>>> //is there GET data?
>>> if($_GET) {
>>> $newgetarr = array();
>>> foreach($_GET as $key => $value) {
>>> if(is_array($value)) {
>>> //if it's an array, convert it to comma separated
>>> $newvalue = implode(',',$value);
>>> } else {
>>> $newvalue = $value;
>>> }
>>> $newgetarr[] = $key.'='.$newvalue;
>>> }
>>> $newget = implode('&',$newgetarr);
>>> $_SESSION['skipnextget'] = 1;
>>> }
>>> foreach($_POST as $key => $value) {
>>> $this[$key] = $value;
>>> }
>>> $_SESSION['postvars'] = serialize($this); //put POST in the session
>>> header("Location: http://"; .$_SERVER['HTTP_HOST']
>>> . $_SERVER['PHP_SELF'] . '?\
>>> ' . $newget);
>>> //reload page without POST data and shorter GET data
>>> 
>>> exit();
>>> } else { //after reloading we'll come right to this spot
>>> session_unregister('skipnextget');
>>> if(isset($_SESSION['postvars'])) {
>>> //load the POST variables from the session
>>> 
>>> // Error on this line below!
>>> $this = unserialize($_SESSION['postvars']);
>>> session_unregister('postvars'); //delete the session stuff we
>>> //don't need anymore.
>>> return $this;
>>> }
>>> }
>>> 
>>> // rest of my code goes here...
>>> _______________________________________________
>>> 
>>> Thanks,
>>> John
>>> john@xxxxxxxxxxx
>> 
>> I'm not sure if it is technically reserved, but $this is used in a class
>> in order for an object to refer to itself.  AFAIK you can't use it
>> anywhere other than that.  Just change all $this to $vars or something else.
>> 

Shawn> Also, as you can see by the use of $this, this is old code.
Shawn> You need to change all of the session_unregister('varname')
Shawn> stuff to unset($_SESSION['varname']). session_unregister() is
Shawn> deprecated and will be removed in PHP 6.

Thanks for the feedback, someone should ask the admins to update some
of the comments on the php.net web pages then, esp since I think
they're trying to kill off 4.x support.

Anyway, I'm wondering if I'm doing this all wrong in some ways.  From
further googling and reading, I think I need to change from my current
setup:

	index.php - form for user to fill in.  POSTS to
	results.php  - checks input (barely :-) does query and shows
		     results.  I'm trying to have results.php redirect
		     to itself and clean out the POST vars.

		     It works, esp since I've got pagination and such
		     for my results pages, etc.  

		     The user can either do a new search, or refine
		     the existing results and goes back to the
		     index.php search page in that case. 

But I think I need to re-work things so that instead I do all form
validation and checks on the index.php page, and once I get all the
$_POST stuff, I redirect to the results.php page, using *only* session
vars.

Though I'm not sure how my page navigation is going to work, since
it's all based on buttons with hidden values.  I guess I should just
change them to session vars in that case...

But back to the $this = unserialize(...) stuff.  I tried your initial
suggestion and renamed it to 

	   $that = unserialize(...)
	   return $that;

and it doesn't work, I get nothing back at all.  So I suspect that
somehow I need to do something different.  

Is there a good reference for building search only forms with multiple
fields, multiple results on a page and sub-pages for detailed results,
etc?

Thanks,
John


	

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux