variables losing value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

please try

Code: Select all

<?php
class GardenMember {
	protected $myProperties = array(); 
	public function __set($name, $value) {
		echo "<div>Debug GardenMember::__set($name, $value)</div>\n";
		$this->myProperties[$name] = $value;
	}

	public function __get($name) {
		if ( isset($this->myProperties[$name]) ) {
			echo "<div>Debug GardenMember::__get($name)={$this->myProperties[$name]}</div>\n";
			return $this->myProperties[$name];
		}
		else {
			echo "<div>Debug GardenMember::__get($name) unknown property<pre>";
			print_r($this->myProperties);
			echo "</pre></div>\n";
			return null;
		}
	}

	function GardenMember() {
		// instantiate the sql object
		$this->sql =& new GardenMember_SQL;
		// instantiate the template object
		$this->tpl =& new GardenMember_Smarty;
	}
and show us the debug output.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

This looks like you are setting a boolean true/false, not a value of a member of the REQUEST array.

Code: Select all

$garparmem->identity = ($_REQUEST['viewkey']);
For giggles, try this:

Code: Select all

<?php
$garparmem->identity = ($_REQUEST['viewkey']);
echo '<pre>; var_dump($garparmem->identity); echo '</pre>';
?>
Then change it to this:

Code: Select all

<?php
$garparmem->identity = $_REQUEST['viewkey'];
echo '<pre>; var_dump($garparmem->identity); echo '</pre>';
?>
And see if it is different.

On a side note, is there a reason you are using $_REQUEST?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Everah wrote:This looks like you are setting a boolean true/false, not a value of a member of the REQUEST array.

Code: Select all

$garparmem->identity = ($_REQUEST['viewkey']);
:?:
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

It was just a thought. I had a script one time (in an environment that I did not control) that would evaluate expressions in parentheses as boolean (sort of like an if parenthesis). It was a freak thing in that environment only, so I was just throwing the idea out there.
kguerrero
Forum Newbie
Posts: 11
Joined: Mon May 28, 2007 8:37 am

Post by kguerrero »

volka, I tried and tried ...

I recieved this error:
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /..../gardenmemtest.lib.php on line 4
which is this line:

Code: Select all

protected $myProperties = array();
for grins I changed "protected" to "var" and even commented out the line:
I would get the same error for the next line of code.

While I did cut and paste your code I did try to resolve the parse error on my own but I did not succeed.

Currently, your code resides in a seperate file which is called out in an include statement. I will clean up my files try again and post all the code.

Everah,

Regarding your requests. I had performed the var_dump on $garparmem->identity through out my code ... it produced the expected result until it was called out in the "reply" case. I will experiment on removing the parathesis. perhaps it is an non-reliable assignment.

The reason I am using the "request" is because my intention is that a user receives an email with a viewkey link. That they would click on. Do you have a better suggestion? I am open to improvement. I didn't use "post" since it is not "posted" from a form - perhaps that is my misunderstanding.

Thanks to all, sorry it took so long to respond when you guys are all over my issue ... I appreciate the support.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The error you are receiving would suggest you are using PHP 4, not 5.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

kguerrero wrote:Everah,

Regarding your requests. I had performed the var_dump on $garparmem->identity through out my code ... it produced the expected result until it was called out in the "reply" case.

The reply case that was in your originally posted code? You know there is no mention of $garparmem->identity anywhere in that case statement.

kguerrero wrote:The reason I am using the "request" is because my intention is that a user receives an email with a viewkey link. That they would click on.
If they are clicking a link they are sending the request through $_GET. I would use that instead if you know it is coming only through the $_GET superglobal.
kguerrero
Forum Newbie
Posts: 11
Joined: Mon May 28, 2007 8:37 am

Post by kguerrero »

Everah,

yes.... I realize it was not in my original posted code ... I had pointed that out to superdezign during this post ... I wish I could start over ... perhaps feyd can help with starting over ... here is all code (minus Smarty engine) associated with what I am seeing.

Also, please notice in this code I removed the paranthesis from $_REQUEST

file name membertest.php

Code: Select all

<?php

define('GARDENPARTY_DIR', '/homepages/31/d200319295/htdocs/site_flash/smarty/gardenparty/');
define('SMARTY_DIR', '/homepages/31/d200319295/htdocs/site_flash/smarty/libs/');
require_once(SMARTY_DIR . 'Smarty.class.php');
include(GARDENPARTY_DIR . 'libs/gardenmem_test.php');

$garparmem=& new GardenMember;

$garparmem->identity = $_REQUEST['viewkey'];

$identity=$garparmem->identity;

$_action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'enter';

switch($_action) {
        case 'enter':
			$garparmem->displaytest($identity);		
		break;
        case 'reply':
			var_dump ($identity);
			die;
        break;
        }

?>
file name: gardenmemtest.lib.php (I have Volka's recommendations commented out)

Code: Select all

<?php

class GardenMember {
//        protected $myProperties = array();
//        public function __set($name, $value) {
//                echo "<div>Debug GardenMember::__set($name, $value)</div>\n";
//                $this->myProperties[$name] = $value;
//        }

//        public function __get($name) {
//                if ( isset($this->myProperties[$name]) ) {
//                        echo "<div>Debug GardenMember::__get($name)={$this->myProperties[$name]}</div>\n";
//                        return $this->myProperties[$name];
//                }
//                else {
//                        echo "<div>Debug GardenMember::__get($name) unknown property<pre>";
//                        print_r($this->myProperties);
//                        echo "</pre></div>\n";
//                        return null;
//                }
//        }
//
        function GardenMember() {
                // instantiate the sql object
                $this->sql =& new GardenMember_SQL;
                // instantiate the template object
                $this->tpl =& new GardenMember_Smarty;
        }
		
		function displaytest($identity){
			$this->tpl->assign ('identity',$identity);
			$this->tpl->display ('memtest.tpl');
		}
}
?>
filename: gardenmem_test.php

Code: Select all

<?php

require_once(GARDENPARTY_DIR . 'libs/sql.lib.php');
require_once(GARDENPARTY_DIR . 'libs/gardenmemtest.lib.php');
require_once(SMARTY_DIR . 'Smarty.class.php');
require('/homepages/31/d200319295/htdocs/site_flash/DB-1.7.11/DB.php');

// database configuration I removed all of this since I am not accessing the db for my experiment
class GardenMember_SQL extends SQL {
    function GardenMember_SQL() {
    }       
}

// smarty configuration
class GardenMember_Smarty extends Smarty { 
    function GardenMember_Smarty() {
        $this->template_dir = GARDENPARTY_DIR . 'templates';
        $this->compile_dir = GARDENPARTY_DIR . 'templates_c';
        $this->config_dir = GARDENPARTY_DIR . 'configs';
        $this->cache_dir = GARDENPARTY_DIR . 'cache';
    }
}
      
?>
Finally, the template used with Smarty ... I don't believe this has anything to do with what I am experiencing.

Filename: memtest.tpl

Code: Select all

<form action="{$SCRIPT_NAME}?action=reply" method="post">
	<table width="350" cellpadding="0" cellspacing="0">
		<tr>
			<td align="right" style="padding-top:2; padding-bottom:8; padding-right:4">{$identity}
			</td>
			<td align="right" style="padding-top:2; padding-bottom:8; padding-right:4"><input type="submit" value="Reply Now">
			</td>
		</tr>
	</table>
</form>
When I run this based on this link:
When the page opens I get the expected result: I see the viewkey next to the reply button.

When I hit "Reply Now" whose action = "reply"

I receive NULL on my browser.

I really apologize that the original code (and most likely the existing code) is not clear.

Thanks to all.

Feyd,

I will need to double check with my host service regarding the PHP version. My host service is 1&1 and they state that they are on PHP 5, but I will double check.


All,

I will also try to use the proper construct standard and see if that helps.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You can run this (in a new file) to confirm what version (among other bits of information.)

Code: Select all

<?php

$neg = array('off', 0, false, '', null);
$flags = array(
	'Register Globals' => 'register_globals',
	'Short Tags' => 'short_open_tag',
	'Display Errors' => 'display_errors',
	'Magic Quotes GPC' => 'magic_quotes_gpc',
	'Magic Quotes Runtime' => 'magic_quotes_runtime',
	'Magic Quotes Sybase' => 'magic_quotes_sybase',
);
$ve = phpversion();
$os = PHP_OS;
$er = intval(error_reporting());
foreach ($flags as $n => $v)
{
	$flags[$n] = (in_array(strtolower(ini_get($v)), $neg) ? 'Off' : 'On');
}
$flags['Config file'] = get_cfg_var('cfg_file_path');
if (empty($flags['Config file']))
{
	$flags['Config file'] = '-';
}
$cli = (php_sapi_name() == 'cli');
$eol = "\n";

$gle = get_loaded_extensions();
$rows = array();
$le = '';
$wide = 4;
$j = count($gle);
$pad = $wide - $j % $wide;
$len = max(array_map('strlen', $gle));
$func = create_function('$a', 'return str_pad($a, ' . intval($len) . ');');
$gle = array_map($func, $gle);
for($i = 0; $i < $j; $i += $wide)
{
	$le .= '   ' . implode('   ', array_slice($gle, $i, $wide)) . $eol;
}

$ec = array(
	'E_STRICT' => 2048, 'E_ALL' => 2047, 'E_USER_NOTICE' => 1024,
	'E_USER_WARNING' => 512, 'E_USER_ERROR' => 256, 'E_COMPILE_WARNING' => 128,
	'E_COMPILE_ERROR' => 64, 'E_CORE_WARNING' => 32, 'E_CORE_ERROR' => 16,
	'E_NOTICE' => 8, 'E_PARSE' => 4, 'E_WARNING' => 2, 'E_ERROR' => 1,
);

$e = array();
$t = $er;
foreach ($ec as $n => $v)
{
	if (($t & $v) == $v)
	{
		$e[] = $n;
		$t ^= $v;
	}
}
if (ceil(count($ec) / 2) + 1 < count($e))
{
	$e2 = array();
	foreach ($ec as $n => $v)
	{
		if (!in_array($n, $e) and $n != 'E_ALL')
		{
			$e2[] = $n;
		}
	}
	$er = $er . ' ((E_ALL | E_STRICT) ^ ' . implode(' ^ ', $e2) . '))';
}
else
{
	$er = $er . ' (' . implode(' | ', $e) . ')';
}

if (!$cli)
{
	echo '<html><head><title>quick info</title></head><body><pre>', $eol;
}

echo 'PHP Version: ', $ve, $eol;
echo 'PHP OS: ', $os, $eol;
echo 'Error Reporting: ', $er, $eol;
foreach ($flags as $n => $v)
{
	echo $n, ': ', $v, $eol;
}
echo 'Loaded Extensions:', $eol, $le, $eol;

if (!$cli)
{
	echo '</pre></body></html>', $eol;
}

?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Your code is working fine. In that link you posted, when you hit reply, you are only sending the var 'action' in the request. There is no form field or querystring var called viewkey. So it is there on page load, but not when you hit that button, so to the script, the second time around, $_REQUEST['viewkey'] is null.

Code: Select all

<form action="?action=reply" method="post">
	<table width="350" cellpadding="0" cellspacing="0">
		<tr>
			<td align="right" style="padding-top:2; padding-bottom:8; padding-right:4">2e30dca200791d2e11e1d721df068950
			</td>
			<td align="right" style="padding-top:2; padding-bottom:8; padding-right:4"><input type="submit" value="Reply Now">
			</td>
		</tr>
	</table>
</form>

If you were to append '&viewkey=<whateverthekeyis.' to the form action the entire thing should work as expected using your original code without any of the proposed changes we have offered.
kguerrero
Forum Newbie
Posts: 11
Joined: Mon May 28, 2007 8:37 am

Post by kguerrero »

Everah, I guess that is what I am confused about.

Am I not taking the value of viewkey and assigning it to the member variable in this line of code:

Code: Select all

$garparmem->identity = $_REQUEST['viewkey'];
then again just for giggles to another variable in this line of code:

Code: Select all

$identity=$garparmem->identity;
So, shouldn't the value of viewkey stay with the variable?

Also, just to be clear: I tried both variables in the var_dump in the reply case.

Seeing as how this was somehow associated with "posting" the form I came up with the work around posted earlier in this Post which utilized hiddend input fields, so the real script is "working" but it doesn't seem right.

so, is

Code: Select all

$garparmem->identity = $_REQUEST['viewkey'];
assigned the value of viewkey or is it the action, or something just occured to me ... (perhaps this is what you were saying I just didn't understand). By hitting reply the entire script is re-run Now this time: $_REQUEST['viewkey'] is NULL therefore all associated variables are re-assigned the Now NULL Value => you enter the reply case with a NULL value.

If this is true then my work around is actually a fix. (I tried to re-post the fix but it must be too far back in this post to get)

Feyd, thanks for the code I will check out my version of PHP.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

OK, let me explain what is happening with that script. When you call the page like you did with the link you posted, there is literally one var set. That var is 'viewkey' and it is set in the $_GET superglobal array (it is also part of $_REQUEST). Once that page loads, you are given the option to click a button which pushes form data to the same page as it is on. When you click the button, you are sending two vars: 'action' which is sent by $_GET and 'submit' which is sent by $_POST (both $_GET and $_POST are part of $_REQUEST). However, you do not send the var 'viewkey' with the button click, so the script does not see the 'viewkey' var anywhere when 'action=reply' because it is never sent. In essence, the script forgets about it as soon as the initial page is displayed.

You see what I'm saying?
Post Reply