Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 15th, 2003, 01:32 PM
Registered User
 
Join Date: Jul 2003
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.
Reply With Quote
  #2 (permalink)  
Old July 15th, 2003, 01:46 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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/
Reply With Quote
  #3 (permalink)  
Old July 15th, 2003, 02:32 PM
Registered User
 
Join Date: Jul 2003
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?


Reply With Quote
  #4 (permalink)  
Old July 15th, 2003, 03:21 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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/
Reply With Quote
  #5 (permalink)  
Old July 16th, 2003, 07:56 PM
Registered User
 
Join Date: Jul 2003
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Reply With Quote
  #6 (permalink)  
Old July 17th, 2003, 01:48 AM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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/
Reply With Quote
  #7 (permalink)  
Old July 17th, 2003, 11:41 PM
Registered User
 
Join Date: Jul 2003
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Reply With Quote
  #8 (permalink)  
Old July 21st, 2003, 02:48 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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/
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chpt 7 >> Pg 245 >>Try It Out #4-5 harrison4411 BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 0 March 2nd, 2006 06:26 PM
Achitecture ? SQL -> XML -> ASP -> PDF or HTML Frm jstrong Classic ASP XML 0 July 9th, 2005 01:18 PM
Chapter 4> ERROR using "=>>>" guiro BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 5 January 13th, 2005 06:38 PM
VB.Net -> Filename -> DTS Package -> tempdB daniel Pro VB.NET 2002/2003 1 October 7th, 2004 01:46 PM
Form -> PHP -> email - how hard can it be?? Capt Jack Sparrow PHP How-To 7 October 6th, 2003 02:00 PM



All times are GMT -4. The time now is 07:44 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.