Moron Posted December 4, 2006 Share Posted December 4, 2006 Sorry to be starting a thread on this every few days, but it's honestly driving me crazy.Short and sweet:[b]First Page:[/b] The employee enters their employee number and password.Works great.[b]Second Page:[/b] Brings up a menu with their name, address, department. Works great.BUT....if they click the "Paystubs" link on this second page, the query fails on the Third Page. If I have them [b]re-enter[/b] employee number and password on the Second Page, them the paystub works fine.So....why aren't my variables, input from the form on Page One, being passed on to Page Three when I click the Paystubs link on Page Two?ALL pages have this at the very top:[code]<?phpsession_start();$_SESSION['empcode'] = $_POST['empcode'];$_SESSION['middle'] = $_POST['middle'];$_SESSION['firstname'] = $_POST['firstname'];$_SESSION['lastname'] = $_POST['lastname'];$_SESSION['leavehours'] = $_POST['leavehours'];$_SESSION['password'] = $_POST['password'];?>[/code]So what gives? Quote Link to comment https://forums.phpfreaks.com/topic/29425-sessions-help-again/ Share on other sites More sharing options...
kenrbnsn Posted December 4, 2006 Share Posted December 4, 2006 The $_POST array is only available when a form has been submitted. If there is no form submittal, then there is no $_POST array.You should only have the session_start() function at the start of all scripts. You should only have the lines[code]<?php$_SESSION['empcode'] = $_POST['empcode'];$_SESSION['middle'] = $_POST['middle'];$_SESSION['firstname'] = $_POST['firstname'];$_SESSION['lastname'] = $_POST['lastname'];$_SESSION['leavehours'] = $_POST['leavehours'];$_SESSION['password'] = $_POST['password'];?>[/code]in scripts that are invoked via a form that pass those values. In scripts that don't get invoked via forms, you probably want to retrieve the values from the session variables:[code]<?php$empcode = $_SESSION['empcode'];$middle = $_SESSION['middle'];$firstname = $_SESSION['firstname'];$lastname = $_SESSION['lastname'];$leavehours = $_SESSION['leavehours'];$password = $_SESSION['password'];?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/29425-sessions-help-again/#findComment-135034 Share on other sites More sharing options...
Moron Posted December 4, 2006 Author Share Posted December 4, 2006 Thanks, Ken. I now have this at the top of my third page, [b]paystubresults.php:[/b][code]<?phpsession_start();$empcode = $_SESSION['empcode'];$middle = $_SESSION['middle'];$firstname = $_SESSION['firstname'];$lastname = $_SESSION['lastname'];$leavehours = $_SESSION['leavehours'];$password = $_SESSION['password'];?>[/code]In my query, I have:[code]WHERE M2.[EMPNO] = '".$_SESSION['empcode']."' and M2.[MSSNO] = '".$_SESSION['password']."'[/code]It still tells me that I've entered an invalid employee number or password (from the "die" statement). Quote Link to comment https://forums.phpfreaks.com/topic/29425-sessions-help-again/#findComment-135043 Share on other sites More sharing options...
sylesia Posted December 4, 2006 Share Posted December 4, 2006 try to output the variables you need to have checked before checking to make sure they were stored and retrieved correctly. Also, make sure that somewhere along the lines something was actually stored in the $_SESSION else they are blank and will not work. Quote Link to comment https://forums.phpfreaks.com/topic/29425-sessions-help-again/#findComment-135068 Share on other sites More sharing options...
Moron Posted December 5, 2006 Author Share Posted December 5, 2006 I've tried echoing the variables on paystubresults.php, but all I get is the results of the "die" statement.Any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/29425-sessions-help-again/#findComment-135468 Share on other sites More sharing options...
kenrbnsn Posted December 5, 2006 Share Posted December 5, 2006 At the top of the script (after the session_start() statement), put the following:[code]<?phpecho '<pre>' . print_r($_SESSION,true) . '</pre>';?>[/code]This is show you what is in the $_SESSION array, if it's not what you think it should be, you will have to follow the data flow to see where the problem is occuring.Ken Quote Link to comment https://forums.phpfreaks.com/topic/29425-sessions-help-again/#findComment-135488 Share on other sites More sharing options...
Moron Posted December 5, 2006 Author Share Posted December 5, 2006 [quote author=kenrbnsn link=topic=117325.msg479090#msg479090 date=1165331975]At the top of the script (after the session_start() statement), put the following:[code]<?phpecho '<pre>' . print_r($_SESSION,true) . '</pre>';?>[/code]This is show you what is in the $_SESSION array, if it's not what you think it should be, you will have to follow the data flow to see where the problem is occuring.Ken[/quote]Hmmm.... maybe we're getting somewhere now. What it gives me is:[quote]Array( [employeenumber] => [password] => ********* (I blanked this out because it's my SSN) [empcode] => [middle] => [firstname] => [lastname] => [leavehours] => )[/quote]So the only entry that has a value is [password], or am I not reading this right?EDIT: Could it be that the problem is having my session variables at the TOP of the page before? Could it be that they don't exist until further down the page?At the top of the second page, I have:[code]<?php session_start();header('Content-Type: image/jpeg');$_SESSION['empcode'] = $_POST['empcode'];$_SESSION['middle'] = $_POST['middle'];$_SESSION['firstname'] = $_POST['firstname'];$_SESSION['lastname'] = $_POST['lastname'];$_SESSION['leavehours'] = $_POST['leavehours'];$_SESSION['password'] = $_POST['password'];?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/29425-sessions-help-again/#findComment-135547 Share on other sites More sharing options...
kenrbnsn Posted December 5, 2006 Share Posted December 5, 2006 If the second page isn't invoked via a form that populates those fields, that would be a problem.Ken Quote Link to comment https://forums.phpfreaks.com/topic/29425-sessions-help-again/#findComment-135614 Share on other sites More sharing options...
craygo Posted December 5, 2006 Share Posted December 5, 2006 You should actually set your session variables at the time you authenticate the user. That way your session variables come right from the table and not a post.[code]<?php$sql = "SELECT * FROM users WHERE empcode = '".$_POST['empcode']."' AND password = '".md5($_POST['password'])."'"; $res = mysql_query($sql) or die (mysql_error());$num_rows = mysql_num_rows($res);$result = mysql_fetch_assoc($res);if($num_rows < 1){echo "No soup for you";} else {$_SESSION['empcode'] = $result['empcode'];$_SESSION['firstname'] = $result['firstname'];// blah blah blah}?>[/code]Now everything will be available right from the time they authenticateRay Quote Link to comment https://forums.phpfreaks.com/topic/29425-sessions-help-again/#findComment-135618 Share on other sites More sharing options...
Moron Posted December 5, 2006 Author Share Posted December 5, 2006 [quote author=kenrbnsn link=topic=117325.msg479217#msg479217 date=1165344560]If the second page isn't invoked via a form that populates those fields, that would be a problem.Ken[/quote]Oops. Sorry. I messed up and pasted from the wrong file.My second page (paystubresults.php) starts with:[code]<?phpsession_start();$empcode = $_SESSION['empcode'];$middle = $_SESSION['middle'];$firstname = $_SESSION['firstname'];$lastname = $_SESSION['lastname'];$leavehours = $_SESSION['leavehours'];$password = $_SESSION['password'];?>[/code]Sorry. I was too quick on the trigger. Quote Link to comment https://forums.phpfreaks.com/topic/29425-sessions-help-again/#findComment-135636 Share on other sites More sharing options...
Moron Posted December 6, 2006 Author Share Posted December 6, 2006 Bump. Any ideas why it's carrying ONLY the password over? Quote Link to comment https://forums.phpfreaks.com/topic/29425-sessions-help-again/#findComment-136213 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.