Wrox Programmer Forums
|
BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143
This is the forum to discuss the Wrox book Beginning PHP 6, Apache, MySQL 6 Web Development by Timothy Boronczyk, Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz; ISBN: 9780470391143
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 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 January 16th, 2010, 04:58 AM
Registered User
 
Join Date: Jan 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem with excercise in page 43 (Ch. 2)

Hi all, guys.

In the excercise, I have this code:
PHP Code:
<?php 
    
//setcookie('username', 'Carlo', time() + 60);
    
session_start();
    
//$_SESSION['username'] = 'Carlo';
    //$_SESSION['authuser'] = 1;
    
$_SESSION['username'] = $_POST['user'];
    
$_SESSION['userpass'] = $_POST['pass'];
    
$_SESSION['authuser'] = 1;
    
    
//Verifica le informazioni relative a password e username
    
if (($_SESSION['username'] == 'Carlo') and ($_SESSION['userpass'] == '12345')) {
        
$_SESSION['authuser'] == 1;
        }
        else {
            echo 
"Spiacente, non hai i permessi per vistare questa pagina";
            exit();
            }
?>

<html>
    <head>
        <title> Cerca il mio film preferito </title>
    </head>
<body>
<?php    
    $filmPref 
urlencode('Detective Stone');
    echo 
"<a href=\"moviesite.php?favmovie=$filmPref\">";
    echo 
"Clicca qui per vedere le informazioni sul mio film preferito";
    echo 
"</a>";
?>
</body>
</html>
At the line 8, I've changed the authuser value form 0 to 1, becouse when I've tried the program with the first value (0), the result was "Sorry, but you don't have permission to view this page". With value 1 I've not this problem.

May be an error code? Thanks.

P.S. I've the italian edition of the book, so I don't know if the excercise page is the same of english edition.
 
Old January 21st, 2010, 04:26 PM
Authorized User
 
Join Date: Jan 2010
Posts: 19
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Hi Luachan,

You may be using a different book... but, the logic of the code you posted appears to be correct...

Code:
    $_SESSION['username'] = $_POST['user'];
    $_SESSION['userpass'] = $_POST['pass'];
    $_SESSION['authuser'] = 0;
This states to get the 'username' and 'userpass' from a form using the variables 'user' and 'pass'. 'authuser' is set to 0 so the information contained within this php script is not accessed by someone who did not log in correctly or simply ran this php script instead of using a form to send information to this php script.

Code:
    if (($_SESSION['username'] == 'Carlo') and ($_SESSION['userpass'] == '12345')) {
This reads: if username is equal to Carlo and the user's password is equal to 12345 then run this code and set authuser to 1: (login is correct)
Code:
        $_SESSION['authuser'] == 1;
        }
The rest of the code will then execute. (The HTML content.)

-----
If the username and password are not correct run this code:
Code:
        else {
            echo "Spiacente, non hai i permessi per vistare questa pagina";
            exit();
            }
Can you post the code for the form that is supposed to send the username and password to this php script?

Thanks
 
Old January 22nd, 2010, 01:14 PM
Authorized User
 
Join Date: Jan 2010
Posts: 19
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Hi Luachan,

Sorry, my mistake... You are using the same book...

You have posted the code from page 45 though.

If you make the changes to moviesite.php, create the new file login.php, then open the login.php with your browser, the code you posted should work.
 
Old January 23rd, 2010, 09:41 AM
Registered User
 
Join Date: Jan 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by s.c. View Post
Hi Luachan,

Sorry, my mistake... You are using the same book...

You have posted the code from page 45 though.

If you make the changes to moviesite.php, create the new file login.php, then open the login.php with your browser, the code you posted should work.
Thanks, S.C.

The code I posted works normally. I had problems with the original code of the exercise in the book that is the following:

PHP Code:
$_SESSION['username'] = $_POST['user'];
    
$_SESSION['userpass'] = $_POST['pass'];
    
$_SESSION['authuser'] = 0
With
PHP Code:
$_SESSION['authuser'] = 0
I can't be able to see the content when I do the log in. Instead, changing the code with
PHP Code:
$_SESSION['authuser'] = 1
, the script works without problems.

So I 'd like to know if there is a problem with the original code or not.

Thansk and sorry for my english: I hope you can understand :-)
 
Old January 23rd, 2010, 12:39 PM
Authorized User
 
Join Date: Jan 2010
Posts: 19
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Hi Luachan,

Can you please post your code for the following:
  • movie1.php
  • moviesite.php
  • login.php

In addition, can you let me know which PHP script you are opening in the web browser and the Username and Password you are using.

-----
Hopefully this has been translated correctly...
-----

Ciao Luachan,

Poterla affigge per favore il suo codice per il seguito:
  • movie1.php
  • moviesite.php
  • login.php

, La può in più fatemi sapere che il manoscritto di PHP lei apre nel browser web ed il Nome utente e Parola d'ordine che lei usa.

Last edited by s.c.; January 23rd, 2010 at 12:42 PM..
 
Old January 24th, 2010, 07:26 AM
Registered User
 
Join Date: Jan 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by s.c. View Post
Hi Luachan,

Can you please post your code for the following:
  • movie1.php
  • moviesite.php
  • login.php

In addition, can you let me know which PHP script you are opening in the web browser and the Username and Password you are using.

-----
Hopefully this has been translated correctly...
-----

Ciao Luachan,

Poterla affigge per favore il suo codice per il seguito:
  • movie1.php
  • moviesite.php
  • login.php

, La può in più fatemi sapere che il manoscritto di PHP lei apre nel browser web ed il Nome utente e Parola d'ordine che lei usa.
S.C., thanks for the translation from english to italian but I can understand very well english, so don't worry :-)

Ok, I post you the code of the files. The first is film1.php (like movie1.php):
PHP Code:
<?php 
    
//setcookie('username', 'Carlo', time() + 60);
    
session_start();
    
//$_SESSION['username'] = 'Carlo';
    //$_SESSION['authuser'] = 1;
    
$_SESSION['username'] = $_POST['user'];
    
$_SESSION['userpass'] = $_POST['pass'];
    
$_SESSION['authuser'] = 1;
    
    
//Verifica le informazioni relative a password e username
    
if (($_SESSION['username'] == 'Carlo') and ($_SESSION['userpass'] == '12345')) {
        
$_SESSION['authuser'] == 1;
        }
        else {
            echo 
"Spiacente, non hai i permessi per vistare questa pagina";
            exit();
            }
?>

<html>
    <head>
        <title> Cerca il mio film preferito</title>
    </head>
<body>
<?php include "header.php"?>
<?php    
    $filmPref 
urlencode('Detective Stone');
    echo 
"<a href=\"moviesite.php?favmovie=$filmPref\">";
    echo 
"Clicca qui per vedere le informazioni sul mio film preferito";
    echo 
"</a>";
?>
<br />
<!--
<a href="moviesite.php?movienum=5"> Clicca qui per vedere i miei 5 film preferiti </a> <br />
<a href="moviesite.php?movienum=10"> Clicca qui per vedere i miei 10 film preferiti </a>

<a href="moviesite.php"> Clicca qui per vedere i miei 10 fil preferiti </a> <br />
<a href="moviesite.php?sorted=true"> Clicca qui per vedere i miei 10 film preferiti in ordine alfabetico </a>
-->
<br />
Oppure scegli quanti film vuoi vedere:
<br />
<form method="post" action="moviesite.php">
<p> Scrivi il numero dei film (fino a 10): <input type="text" name="num" maxlength="2" size="2" />
<br />
Prova a ordinarli alfabeticamente: <input type="checkbox" name="sorted" />
</p>
<input type="submit" name="submit" value="Invia" />

<?php include('autore.php'); ?>
</body>
</html>
The next is moviesite.php:
PHP Code:
<?php
    session_start
();
    
    
//verifica se l'utente ha fatto login con una password valida
    
if ($_SESSION['authuser'] != 1) {
        echo 
"Spiacente, ma non hai i permessi per vedere questa pagina";
        exit();
        }
?>

<html>
    <head>
        <title>
            <?php 
                
if (isset($_GET['favmovie'])) {
                    echo 
" - ";
                    echo 
$_GET['favmovie'];
                }
            
?>
<body>
<?php include "header.php"?>

<?php
    $favmovies 
= array('Detective Stone',
                       
'Il Signore degli Anelli',
                       
'Napoli violenta',
                       
'Pulp Fiction',
                       
'Full Metal Jacket',
                       
'Fantozzi',
                       
'Dracula',
                       
'Matrix',
                       
'Rainman',
                       
'Il giustiziere della notte');                   
    
    
//Queste sono le funzioni
    /* function listmovie_1() {
        echo "1. Detective Stone" . "<br />";
        echo "2. Il Signore degli Anelli" . "<br />";
        echo "3. Napoli violenta" . "<br />";
        echo "4. Pulp Fiction" . "<br />";
        echo "5. Full Metal Jacket" ."<br />";
        }
        
    function listmovie_2() {
        echo "6. Fantozzi" . "<br />";
        echo "7. Dracula" . "<br />";
        echo "8. Matrix" . "<br />";
        echo "9. Rainman" . "<br />";
        echo "10. Il giustiziere della notte" . "<br />";
        }
    */
    
    
if (isset($_GET['favmovie'])) {
        echo 
"Benvenuto nel mio sito, ";
        
//echo $_COOKIE['username'];
        
echo $_SESSION['username'];
        echo 
"! <br />";
        
//define ('FAVMOVIE', 'Detective Stone');
        
echo 'Il mio film preferito Ã¨ ';
        
//echo FAVMOVIE;
        
echo $_GET['favmovie'];
        echo 
'<br />';
        
$votoFilm 5;
        echo 
'Il mio voto a questo film Ã¨ ' $votoFilm;
    }
    else 
    {
        echo 
"La mia top " $_POST['num'] . " cinematografica preferita";
        if (isset(
$_GET['sorted'])) {
            
sort($favmovies);
            echo 
" (ordinata alfabeticamente) ";
            }
            echo 
" Ã¨: <br />";
            
    
/*    echo "<ol>";
        foreach ($favmovies as $movie) {
            echo "<li>";
            echo $movie;
            echo "</li>";
            }
        echo "</ol>"; */
            
        /*echo "La mia top ";
        echo $_GET['movienum'];
        echo " dei film preferiti Ã¨ la seguente:";
        echo "<br />";
        
        listmovie_1();
        if ($_GET['movienum'] == 10) {
            listmovie_2();
            } 
        */
        
        
$numlist 0;
        echo 
"<ol>";
        while (
$numlist $_POST['num']) {
            echo 
"<li>";
            echo 
$favmovies[$numlist];
            echo 
"</li>";
            
$numlist $numlist 1;
            }
        echo 
"</ol>";
        }
?>
<?php 
include('autore.php'); ?>
</body>
</html>
The last is login.php:
PHP Code:
<?php
    session_unset
();
?>
<html>
    <head>
        <title>Login</title>
    </head>
<body>
<?php include "header.php"?>
    <form method="post" action="film1.php">
        <p>Nome: <input type="text" name="user" /> </p>
        <p>Pass: <input type="password" name="pass" /> </p>
        <p> <input type="submit" name="submit" value="Invia" />
    </form>
<?php include('autore.php'); ?>
</body>
</html>
User is 'Carlo' and password is '12345'
 
Old January 24th, 2010, 03:37 PM
Authorized User
 
Join Date: Jan 2010
Posts: 19
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Hi Luachan,

Quote:
S.C., thanks for the translation from english to italian but I can understand very well english, so don't worry :-)
Thanks! :)

Ok, I think I know where the problems are, I was able to get your code to work after making the following changes:

In film1.php:
Code:
<?php 
    //setcookie('username', 'Carlo', time() + 60);
    session_start();
    //$_SESSION['username'] = 'Carlo';
    //$_SESSION['authuser'] = 1;
    $_SESSION['username'] = $_POST['user'];
    $_SESSION['userpass'] = $_POST['pass'];
    $_SESSION['authuser'] = 1;          <<< Change from 1 to 0
    
    //Verifica le informazioni relative a password e username
    if (($_SESSION['username'] == 'Carlo') and ($_SESSION['userpass'] == '12345')) {
        $_SESSION['authuser'] == 1;      <<< Change from == to =
        }
        else {
            echo "Spiacente, non hai i permessi per vistare questa pagina";
            exit();
            }
?>

In moviesite.php:
Code:
<html>
    <head>
        <title>
            <?php 
                if (isset($_GET['favmovie'])) {
                    echo " - ";
                    echo $_GET['favmovie'];
                }
            ?>
         </title>        <<< Add to your code 
    </head>   <<<  Add to your code
<body>

When I made these changes, I was able to get your code to work on my machine.
 
Old January 25th, 2010, 11:22 AM
Registered User
 
Join Date: Jan 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

S.C., thanks a lot: now the script runs without problems.
I did stupid mistakes
 
Old January 25th, 2010, 03:02 PM
Authorized User
 
Join Date: Jan 2010
Posts: 19
Thanks: 0
Thanked 4 Times in 4 Posts
Default

You're welcome. Sometimes the simple mistakes create the biggest headaches...





Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with page 42 - 43 of the text Whiteetea BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 2 October 28th, 2008 10:51 PM
Listing 23-43 wombel BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 0 November 30th, 2006 09:40 AM
Excercise answers for book GrantWillie C# 0 October 22nd, 2003 05:07 AM
C#,chapter 8 excercise akbard BOOK: Beginning ASP.NET 1.0 2 October 7th, 2003 03:25 AM





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