Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
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
Old May 24th, 2008, 12:25 PM
Authorized User
Join Date: May 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default $_SESSION error in login.php


I'm using the 'Beginning PHP5,Apache,And MySQL Web development' and on pages 55-56, you develop a login page for you movies site, every time I try to login I get this error message. it's supposed to load another page up, but doesn't and loads this error message. The lines in the message refer to:
$_SESSION['username'] = $_POST['user']; // line 5
$_SESSION['userpass'] = $_POST['pass']; // line 6

Welcome to my movie review site!
Today is May 24, 2008

Notice: Undefined index: user in C:\wamp\www\PHP_files\movie1.php on line 5

Notice: Undefined index: pass in C:\wamp\www\PHP_files\movie1.php on line 6
Sorry, but you don't have permission to view this page, you loser!

Thanks for any help

Old June 2nd, 2008, 03:43 PM
Friend of Wrox
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts

I haven't got the book, but I think I've found the source code you are using:
$_SESSION['username'] = $_POST['user'];
$_SESSION['userpass'] = $_POST['pass'];
$_SESSION['authuser'] = 0;

//Check username and password information
if (($_SESSION['username'] == 'Joe') &&
    ($_SESSION['userpass'] == '12345')) {
  $_SESSION['authuser'] = 1;
} else {
  echo "Sorry, but you don't have permission to view this page, you loser!";

When you run this page, PHP will look for POSTed (form textbox) values named user and pass. If you run it directly without going via login.php, these obviously will not exist, and so you will get the error above.
Also double check that your login.php has <form method="post"> as opposed to method="get" and that the names of the fields correspond to the ones you are using in movie1.php (user and pass).

As an aside, you can add a simple check to make sure the POST values are set before you use them:


if(isset($_POST['user'])) {
  // if the index 'user' is set (exists) in the $_POST array, then use it
  $_SESSION['username'] = $_POST['user'];
else {
  // was not set, so set to a empty value
  $_SESSION['username'] = '';

// do the same for password
if(isset($_POST['pass'])) {
  $_SESSION['userpass'] = $_POST['pass'];
else {
  $_SESSION['userpass'] = '';

$_SESSION['authuser'] = 0;

//Check username and password information as before ...


Similar Threads
Thread Thread Starter Forum Replies Last Post
login php Case Problem saifi4u PHP How-To 3 March 7th, 2007 12:58 AM
$_Session cant pass Value kelz84 Beginning PHP 1 March 21st, 2006 09:25 AM
Difficulty with $_SESSION mall9@hotmail.com Beginning PHP 11 March 11th, 2006 05:41 PM
No login with auth_user.php stefanocinfo BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 0 September 10th, 2005 03:34 AM
$_SESSION vs. $_GLOBALS Moharo Pro PHP 2 August 18th, 2003 02:27 PM

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