Session Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

jrmontg
Forum Newbie
Posts: 12
Joined: Mon Apr 05, 2004 9:59 am

Session Problem

Post by jrmontg »

I am having a problem with sessions:

I am using Apache, PHP, APACHE on Windows XP pro.

I have a login page that creates a session. After loggin in a wanted to display the session.

Funny thing is that in Netscape it works fine but in IE6 I get this error:

Notice: Undefined index: Username in C:\Apache\Apache Group\Apache2\htdocs\userareas\home.php on line 120

HELP!?

Thanks
Jeff
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

What's the code? Is Username in the $_SESSION array or $_POST array? and are you sure its supposed to be capitalized?
jrmontg
Forum Newbie
Posts: 12
Joined: Mon Apr 05, 2004 9:59 am

Post by jrmontg »

Ok yes they were different. One was username and the other was Username. I changed it to lower case.

Getting the error: Notice: Undefined index: username in C:\Apache\Apache Group\Apache2\htdocs\userareas\home.php on line 120

This is from the login page:

} else {
mysql_free_result($rsLogin);
session_register("username");
$HTTP_SESSION_VARS['username'] = $HTTP_POST_VARS['username'];
header("Location: userareas/home.php");
}

On the next page I am displaying the session var with:

<?php echo $_SESSION['username']; ?>

Which is line 120.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

try

$_SESSION['username'] = $_POST['username'];

instead of

$HTTP_SESSION_VARS['username'] = $HTTP_POST_VARS['username'];

I think those are no more and I think you can lose the

session_register("username");
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

magicrobotmonkey wrote:try

$_SESSION['username'] = $_POST['username'];

instead of

$HTTP_SESSION_VARS['username'] = $HTTP_POST_VARS['username'];

I think those are no more and I think you can lose the

session_register("username");
exactly :D
jrmontg
Forum Newbie
Posts: 12
Joined: Mon Apr 05, 2004 9:59 am

Post by jrmontg »

I changed my login page:

mysql_free_result($rsLogin);
$_SESSION['username'] = $_POST['username'];
header("Location: userareas/home.php");

It still works in netscape but not IE6. Get error:

Notice: Undefined index: username in C:\Apache\Apache Group\Apache2\htdocs\userareas\home.php on line 117

Weird
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

try loosing the header code.

JS works good and i've had problems with header outputs.
echo "<script language=javascript>location.replace('index.php')</script>";

Also - for giggles, you do have a session_start(); at the top of the page?
jrmontg
Forum Newbie
Posts: 12
Joined: Mon Apr 05, 2004 9:59 am

Post by jrmontg »

Yes, the session start is there.

Made the change:

$_SESSION['username'] = $_POST['username'];
echo "<script language=javascript>location.replace('userareas/home.php')</script>";

Still I get the same results.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

You have session_start on both pages? And you get no errors on the first page - you're sure $_POST['username'] is set?
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

then my guess would be you didnt set the variable username.

lets see that portion of code
jrmontg
Forum Newbie
Posts: 12
Joined: Mon Apr 05, 2004 9:59 am

Post by jrmontg »

There is a error on the login page. Sorry I didn't see it because I have a form on a different page and it just goes to the login page to validate. The error is:

Notice: Undefined variable: errorMessage in C:\Apache\Apache Group\Apache2\htdocs\login.php on line 55

Code:
<?php
session_start();
?>
<?php require_once('Connections/connUserareas.php'); ?>
<?php

$myUsername_rsLogin = "0";
if (isset($HTTP_POST_VARS['username'])) {
$myUsername_rsLogin = (get_magic_quotes_gpc()) ? $HTTP_POST_VARS['username'] : addslashes($HTTP_POST_VARS['username']);
}
$myPassword_rsLogin = "0";
if (isset($HTTP_POST_VARS['password'])) {
$myPassword_rsLogin = (get_magic_quotes_gpc()) ? $HTTP_POST_VARS['password'] : addslashes($HTTP_POST_VARS['password']);
}
mysql_select_db($database_connUserareas, $connUserareas);

$query_rsLogin = sprintf("SELECT Username, Password FROM companies WHERE Username = '%s' AND Password = '%s'", $myUsername_rsLogin,$myPassword_rsLogin);
$rsLogin = mysql_query($query_rsLogin, $connUserareas) or die(mysql_error());
$row_rsLogin = mysql_fetch_assoc($rsLogin);
$totalRows_rsLogin = mysql_num_rows($rsLogin);


if($HTTP_POST_VARS['action']=="login"){
if($totalRows_rsLogin==0){
$errorMessage = "Your Login Information is incorrect";
mysql_free_result($rsLogin);
} else {
mysql_free_result($rsLogin);
$_SESSION['username'] = $_POST['username'];
echo "<script language=javascript>location.replace('userareas/home.php')</script>";
}
}

error_reporting(E_ALL);
ini_set('display_errors', true);
?>
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

again, change $HTTP_POST_VARS to $_POST

the current error is from $errorMessage not being set because the outermost if statement it is in will never be true becasue $HTTP_POST_VARS['action'] is not set

also - you open and close php tages three times with nothing in between. Is that because you're cutting and pasting onto here?
Last edited by magicrobotmonkey on Mon Apr 05, 2004 11:23 am, edited 1 time in total.
jrmontg
Forum Newbie
Posts: 12
Joined: Mon Apr 05, 2004 9:59 am

Post by jrmontg »

Ok, done.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

does it work?
jrmontg
Forum Newbie
Posts: 12
Joined: Mon Apr 05, 2004 9:59 am

Post by jrmontg »

Sorry,

No I still get a notice on the login page.
Notice: Undefined variable: errorMessage in C:\Apache\Apache Group\Apache2\htdocs\login.php on line 53

And still having the problem on the next page with IE.
Post Reply