Jump to content

[SOLVED] login function - settign session variables inside a function?


bdee1

Recommended Posts

hi - i am trying to do a simple login for my site and and tryign to build functions in a seperate functions.php file for common tasks.  so i created a login function which take the username and password as arguments and looks in the database for a match.  if it finds a match it creates the session variables for the user.

$_SESSION['loggedIn'] = 1;
$_SESSION['username'] = $$username;
$_SESSION['firstName'] = $FirstName;
$_SESSION['lastName'] = $LastName;

 

on the login.php page that calls the function, it checks to see if the login was successful and if so redirects to the main page (index.php).... that much works fine.

 

but index.php does the following:

if(empty($_SESSION['loggedIn']) || empty($_SESSION['username']))  
{  
header('Location: login.php');   
}
else
{
     #Display the main page content
}

 

and for some reason the test test for the existence fo the session variables fails and it kicks them back to the login page.

 

why cant index.php see the session variables?  does it have anything to do with the face that i am defining the session variables inside a function?

 

any guidance here would be appreciated.

it needs to be on any page that wants to read from/write to the session. it should be the first thing on the page too. if you include a file at the beginning of every script, you can put it in that file if you want.

It MUST go before the HTML tags.

 

so it shodul go even before the html tags?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

dangit - i just added it to the login.php page and i get the following errors:

 

Node no longer exists in C:\...\common.php on line 2

 

common.php is the file i am including that has session_start(0) in it.

 

the code for common.php is:

<?php
session_start();  
?>

what are you storing in the SESSION? looking back i notice that this:

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

should probably be:

$_SESSION['username'] = $username;

 

are you storing anything besides simple numbers/strings? like are you trying to store objects, file handles, db connections, or xml nodes?

here is my actual code...

$_SESSION['loggedIn'] = 1;
$_SESSION['username'] = $user->username;
$_SESSION['firstName'] = $user->FirstName;
$_SESSION['lastName'] = $user->LastName;

 

i simplified it before because i didn't think the xml part was relevant.  how do i need to change this to store the valued of the nodes and not the actual nodes?

tried forcing them to strings and it didnt seem to change anything - i still get the same error.

 

$_SESSION['loggedIn'] = 1;
$_SESSION['username'] = (string)$user->username;
$_SESSION['firstName'] = (string)$user->FirstName;
$_SESSION['lastName'] = (string)$user->LastName;

destroy you session by either deleting your cookies or closing your browser and re-opening it

 

edit: if you ARE using the following:

$_SESSION['loggedIn'] = 1;
$_SESSION['username'] = (string)$user->username;
$_SESSION['firstName'] = (string)$user->FirstName;
$_SESSION['lastName'] = (string)$user->LastName;

then print_r($_SESSION) shouldn't have SimpleXMLElement anywhere in it

i just cleared my cookies so that the session would get reset and now i get the following output:

Array ( )

 

with eh following error message:

PHP Notice: A session had already been started - ignoring session_start() in C:\...\common.php on line 2

 

PHP Fatal error: Cannot redeclare deleteitem() (previously declared in C:\...\functions.php:6) in C:\...\functions.php on line 38

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.