Wrox Programmer Forums
|
BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5
This is the forum to discuss the Wrox book Beginning PHP4 by Wankyu Choi, Allan Kent, Chris Lea, Ganesh Prasad, Chris Ullman; ISBN: 9780764543647
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 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
 
Old October 5th, 2003, 09:57 AM
Authorized User
 
Join Date: Sep 2003
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default page_count.

i have problems with the example of page_count.php in chapter 8. I think that I can resolve this problem with $_SESSION[] variable, but I don't understand how this command work. Can you help me? My code is:

<?php
session_register ("guarda1contatore");
session_register ("guarda2contatore");
session_register ("guarda3contatore");
session_register ("guarda4contatore");
?>
<?php
if (!$guarda1contatore) {$guarda1contatore = 0;}
if (!$guarda2contatore) {$guarda2contatore = 0;}
if (!$guarda3contatore) {$guarda3contatore = 0;}
if (!$guarda4contatore) {$guarda4contatore = 0;}

echo "<html><head><title></title></head><body>";
if ($pagina) {
    echo "<b>ora sei alla pagina $pagina </b><br><br>";
    $GLOBALS["guarda${pagina}contatore"]++;
}
for ($i = 1; $i <= 4; $i++) {
    if ($pagina == $i) {
    echo "<b><a href=\"$PHP_SELF?" . SID . "pagina=$i\">pagina $i</a></b>";
    }
    else {
    echo "<a href=\"$PHP_SELF?" . SID . "pagina=$i\">pagina $i<a>";
    }
    echo " : totale visite" . $GLOBALS["guarda${i}contatore"] . "<br>";
    echo "</body></html>";
}

 
Old October 5th, 2003, 03:07 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Hi Joeore,
The problem relates wholly to register_globals.

First some URLs for you to reference:
Why session_register() is bad:
http://p2p.wrox.com/topic.asp?TOPIC_ID=2052

Nik's FAQ:
http://p2p.wrox.com/archive/beginnin...2002-11/17.asp

And here's a long thread that contains many more urls to reference on both php.net on the wrox forums:
http://p2p.wrox.com/topic.asp?TOPIC_ID=4017

Code:
<?php

# Instead of session_register, use the $_SESSION[] superglobal
# If $_SESSION["..."] is not set, then create it.

if (!isset($_SESSION["guarda1contatore"])) {$_SESSION["guarda1contatore"] = 0;}
if (!isset($_SESSION["guarda2contatore"])) {$_SESSION["guarda2contatore"] = 0;}
if (!isset($_SESSION["guarda3contatore"])) {$_SESSION["guarda3contatore"] = 0;}
if (!isset($_SESSION["guarda4contatore"])) {$_SESSION["guarda4contatore"] = 0;}

echo "<html><head><title></title></head><body>";

# pagina is part of a URL query string and thus accessible via the $_GET superglobal

if (isset($_GET["pagina"])) {

    echo "<span style='font-weight: bold;'>ora sei alla pagina {$_GET["pagina"]}</span><br /><br />";
    $_SESSION["guarda{$_GET["pagina"]}contatore"]++;

}

for ($i = 1; $i <= 4; $i++) {

    if ($_GET["pagina"] == $i) {

        # Use $_SERVER["PHP_SELF"] instead of $PHP_SELF

        echo "<span style='font-weight: bold;'><a href=\"{$_SERVER["PHP_SELF"]}?" . SID . "pagina=$i\">pagina $i</a></span>";

    } else {

        echo "<a href=\"{$_SERVER["PHP_SELF"]}?" . SID . "pagina=$i\">pagina $i</a>";

    }
    echo " : totale visite" . $_SESSION["guarda${i}contatore"] . "<br />";
}
    echo "</body></html>";

?>
I also have a nifty HTML standards compliance FAQ:
http://p2p.wrox.com/topic.asp?TOPIC_ID=4028

Test that out and see what it does. I haven't tested for parse errors.

: )
Rich


:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old October 5th, 2003, 03:13 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Oh and one more conversion that I missed!

if (isset($_GET["pagina"])) {

    echo "<span style='font-weight: bold;'>ora sei alla pagina {$_GET["pagina"]}</span><br /><br />";
    $_SESSION["guarda{$_GET["pagina"]}contatore"]++;

}

hth
: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old October 5th, 2003, 03:29 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Also it would be helpful to create a default value for $_GET["pagina"]; if none is provided.

if (!isset($_GET["pagina"]))
{
    $_GET["pagina"] = 1;
}

if (isset($_GET["pagina"])) {

    echo "<span style='font-weight: bold;'>ora sei alla pagina {$_GET["pagina"]}</span><br /><br />";
    $_SESSION["guarda{$_GET["pagina"]}contatore"]++;

}

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old October 5th, 2003, 05:28 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You're all forgetting the most important step: You need to call session_start() before you can access any session variables.


Take care,

Nik
http://www.bigaction.org/
 
Old October 6th, 2003, 12:18 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Thanks for pointing that out Nik,
I guess when I looked at the post originally I assumed it was just a snippet.. but it is definitely worth mentioning.

Joeore-
When you include the call to session_start() in your code it should appear before all other content, as this function sends out a cookie in the outgoing HTTP response from the server. This means that it needs to be called before any output e.g. echo or print before the opening HTML tag. And the opening PHP delimiter <?php may not have whitespace before it.

http://www.php.net/session_start

: )
Rich


:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::









Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.