bdee1 Posted January 16, 2009 Share Posted January 16, 2009 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. Link to comment https://forums.phpfreaks.com/topic/141097-solved-login-function-settign-session-variables-inside-a-function/ Share on other sites More sharing options...
rhodesa Posted January 16, 2009 Share Posted January 16, 2009 are you sure session_start() is being run? there shouldn't be any problem with setting session variables inside a function as $_SESSION is automatically global Link to comment https://forums.phpfreaks.com/topic/141097-solved-login-function-settign-session-variables-inside-a-function/#findComment-738492 Share on other sites More sharing options...
bdee1 Posted January 16, 2009 Author Share Posted January 16, 2009 thats what it was!!! thanks!! one more question though... do i need to put session_start() on every page? where do i need to include that? Link to comment https://forums.phpfreaks.com/topic/141097-solved-login-function-settign-session-variables-inside-a-function/#findComment-738496 Share on other sites More sharing options...
rhodesa Posted January 16, 2009 Share Posted January 16, 2009 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. Link to comment https://forums.phpfreaks.com/topic/141097-solved-login-function-settign-session-variables-inside-a-function/#findComment-738497 Share on other sites More sharing options...
bdee1 Posted January 16, 2009 Author Share Posted January 16, 2009 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"> Link to comment https://forums.phpfreaks.com/topic/141097-solved-login-function-settign-session-variables-inside-a-function/#findComment-738502 Share on other sites More sharing options...
revraz Posted January 16, 2009 Share Posted January 16, 2009 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"> Link to comment https://forums.phpfreaks.com/topic/141097-solved-login-function-settign-session-variables-inside-a-function/#findComment-738503 Share on other sites More sharing options...
bdee1 Posted January 16, 2009 Author Share Posted January 16, 2009 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(); ?> Link to comment https://forums.phpfreaks.com/topic/141097-solved-login-function-settign-session-variables-inside-a-function/#findComment-738505 Share on other sites More sharing options...
rhodesa Posted January 16, 2009 Share Posted January 16, 2009 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? Link to comment https://forums.phpfreaks.com/topic/141097-solved-login-function-settign-session-variables-inside-a-function/#findComment-738507 Share on other sites More sharing options...
bdee1 Posted January 16, 2009 Author Share Posted January 16, 2009 fixed the typo.. well i am tryign to store the values from xml nodes - not the nodes object itself. Link to comment https://forums.phpfreaks.com/topic/141097-solved-login-function-settign-session-variables-inside-a-function/#findComment-738509 Share on other sites More sharing options...
rhodesa Posted January 16, 2009 Share Posted January 16, 2009 you will have problems storing the nodes....force them to strings...assuming $username is an XML node: $_SESSION['username'] = (string)$username; Link to comment https://forums.phpfreaks.com/topic/141097-solved-login-function-settign-session-variables-inside-a-function/#findComment-738511 Share on other sites More sharing options...
bdee1 Posted January 16, 2009 Author Share Posted January 16, 2009 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? Link to comment https://forums.phpfreaks.com/topic/141097-solved-login-function-settign-session-variables-inside-a-function/#findComment-738514 Share on other sites More sharing options...
bdee1 Posted January 16, 2009 Author Share Posted January 16, 2009 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; Link to comment https://forums.phpfreaks.com/topic/141097-solved-login-function-settign-session-variables-inside-a-function/#findComment-738515 Share on other sites More sharing options...
rhodesa Posted January 16, 2009 Share Posted January 16, 2009 what is the output of: print_r($_SESSION); Link to comment https://forums.phpfreaks.com/topic/141097-solved-login-function-settign-session-variables-inside-a-function/#findComment-738517 Share on other sites More sharing options...
bdee1 Posted January 16, 2009 Author Share Posted January 16, 2009 Array ( [loggedIn] => 1 [username] => SimpleXMLElement Object ( ) [firstName] => SimpleXMLElement Object ( ) [lastName] => SimpleXMLElement Object ( ) ) Link to comment https://forums.phpfreaks.com/topic/141097-solved-login-function-settign-session-variables-inside-a-function/#findComment-738521 Share on other sites More sharing options...
rhodesa Posted January 16, 2009 Share Posted January 16, 2009 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 Link to comment https://forums.phpfreaks.com/topic/141097-solved-login-function-settign-session-variables-inside-a-function/#findComment-738535 Share on other sites More sharing options...
bdee1 Posted January 16, 2009 Author Share Posted January 16, 2009 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 Link to comment https://forums.phpfreaks.com/topic/141097-solved-login-function-settign-session-variables-inside-a-function/#findComment-738537 Share on other sites More sharing options...
bdee1 Posted January 16, 2009 Author Share Posted January 16, 2009 ok nevermind - i found the problem - i accidentally included common.php and functions.php twice in one of my files. seems to be working now. thanks so much for your help!! Link to comment https://forums.phpfreaks.com/topic/141097-solved-login-function-settign-session-variables-inside-a-function/#findComment-738545 Share on other sites More sharing options...
rhodesa Posted January 16, 2009 Share Posted January 16, 2009 using include_once() or require_once() will solve that issue too Link to comment https://forums.phpfreaks.com/topic/141097-solved-login-function-settign-session-variables-inside-a-function/#findComment-738547 Share on other sites More sharing options...
bdee1 Posted January 16, 2009 Author Share Posted January 16, 2009 thanks for the tip! seems to be working great now! Link to comment https://forums.phpfreaks.com/topic/141097-solved-login-function-settign-session-variables-inside-a-function/#findComment-738557 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.