 |
| Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

July 15th, 2003, 01:32 PM
|
|
Registered User
|
|
Join Date: Jul 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
php.ini -> regisiter globals -> On = BAD?
Hey All,
When turn on the register globals this code works fine:
<?
session_start();
?>
<?
session_register("v1c");
session_register("v2c");
session_register("v3c");
session_register("v4c");
?>
<?
$whichpage = $_GET[whichpage];
if (!$v1c) $v1c = 0;
if (!$v2c) $v2c = 0;
if (!$v3c) $v3c = 0;
if (!$v4c) $v4c = 0;
echo "<html><head><title>Web Page Hits</title></head><body>";
if ($whichpage) {
echo "<b>You are currently on page $whichpage.</b><br><br>\n";
$GLOBALS["v${whichpage}c"]++;
}
for ($i = 1; $i <=4; $i++) {
echo "<a href=\"$PHP_SELF?whichpage=$i&PHPSESSID=.PHPSESSID ."\">Page $i</a>";
echo ", which you have choosen ".$GLOBALS["v${i}c." times.<br>\n";
}
echo "\n\n<br><br>\n\n";
echo "</body></html>";
?>
However I read some place that it is bad to have the register globals set to on. And that I needed to set the variable with a $_GET or $_POST.
So I set it to off and now only part of the code works.
It no longer counts the page hits.
It seems to loose the PHPSESSID, I think.
Is there something else that needs to be done and/or set?
Any help would be great.
TIA
troinfo.
|
|

July 15th, 2003, 01:46 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
After answering hundreds of register_globals questions last year, I decided to post a FAQ: http://p2p.wrox.com/archive/beginnin...2002-11/17.asp
You need to use $_SERVER['PHP_SELF'] instead of $PHP_SELF.
I also strongly suggest you ditch using session_register() and start using $_SESSION.
Therefore, your session variable, $v1c, should be $_SESSION['v1c']. I don't see anywhere in your code where PHPSESSID is used in a valid PHP context -- the only line in which you reference it is in a double-quoted string, which means the token is taken literally as part of the string and NOT extracted as a defined constant:
Quote:
quote:
echo "<a href=\"$PHP_SELF?whichpage=$i&PHPSESSID=.PHPSESSID ."\">Page $i</a>";
|
Count your quotes carefully here -- You enter the string, escape the quote surrounding $PHP_SELF, then end the quote [b]after[b] ".PHPSESSID.". The next token is an escaped quote, which doesn't make sense outside of string context. You're code should cause a syntax or parse error, it's missing a double-quote before the ".PHPSESSID.".
Take care,
Nik
http://www.bigaction.org/
|
|

July 15th, 2003, 02:32 PM
|
|
Registered User
|
|
Join Date: Jul 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for the quick answer.
The $_SERVER['PHP_SELF'] Throws an error> Parse error: parse error, unexpected T_ENCAPED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /session.php on line 30.
I changed it to $_SERVER[PHP_SELF] and then echoed it and the correct location was shown.
Ran the script with out any errors. Not sure if that is good or bad.
I also put in the $_SESSION['v1c'] etc; ran it and the script did not count the pages still.
The missing quote was a typo in the post. It was not in my code, that was a good catch though.
Anythng else I can do?
|
|

July 15th, 2003, 03:21 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
So what is still not working? I suggest, for debugging purposes, you look at the variables you're dealing with. Here is the most useful function I've ever written:
Code:
function printr(&$var, $desc = '')
{
echo '<pre>';
if($desc != '') echo "$desc:";
print_r($var)
echo "<pre>\n";
}
Some usage examples:
printr($_POST, '$_POST'); // single-quoted to avoid var substitution
printr($_SESSION, '$_SESSION, before adding foo');
$_SESSION['foo'] = "Hello, world!";
printr($_SESSION, '$_SESSION, after adding foo');
etc.
Have you completely ditched session_register()? If you're still using session register() and are incrementing a global, that global is NOT necessarily the variable stored in $_SESSION.
Take care,
Nik
http://www.bigaction.org/
|
|

July 16th, 2003, 07:56 PM
|
|
Registered User
|
|
Join Date: Jul 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hey Thanks for your help,
BTW I did get rid of the register thingy's. still no joy. Oh well I'll keep trying.
Do you know if they are coming out with a PHP for Beginners Second Edition?
Thanks,
troinfo
|
|

July 17th, 2003, 01:48 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Post your most recent code snapshot, if I have time I'll poke through it and see if there's anything else amiss.
One thing, though -- your array index is a string, so change the following line to the next:
$whichpage = $_GET[whichpage];
should be
$whichpage = $_GET['whichpage'];
Take care,
Nik
http://www.bigaction.org/
|
|

July 17th, 2003, 11:41 PM
|
|
Registered User
|
|
Join Date: Jul 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Ok,
I got it working like this:
<?
session_start();
$_SESSION[v1c];
$_SESSION[v2c];
$_SESSION[v3c];
$_SESSION[v4c];
$whichpage = $_GET[whichpage];
if(!$_SESSION[v1c]) $_SESSION[v1c] = 0;
if(!$_SESSION[v2c]) $_SESSION[v2c] = 0;
if(!$_SESSION[v3c]) $_SESSION[v3c] = 0;
if(!$_SESSION[v4c]) $_SESSION[v4c] = 0;
echo "<html><head><title>Web page hits</title></head><body>";
if ($whichpage) {
echo "<b>you are currently on page $whichpage.</b><br><br>\n";
$_SESSION["v${whichpage}c"]++;
}
for ($i = 1; $i <= 4; $i++) {
echo "<a href=\"$_SERVER[PHP_SELF]?whichpage=$i&PHPSESSID=".$PHPSESSID."\">Page $i</a>";
echo ", which you have choosen ".$_SESSION["v${i}c"]." times.<br>\n";
}
echo "\n\n<br><br>\n\n";
echo "</body></html>";
?>
Thanks Nik for all you help,
troinfo
|
|

July 21st, 2003, 02:48 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Okay, there's still room for improvement:
You don't need the variable declarations in lines 3-7 of your script. They're useless because they don't DO anything.
Also, you're still not being very explicit in your code. Don't be lazy!
if(!$_SESSION['v1c'])
should be
if(!isset($_SESSION['v1c']))
Take care,
Nik
http://www.bigaction.org/
|
|
 |