Jump to content

Recommended Posts

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]
<?php
session_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?

Link to comment
https://forums.phpfreaks.com/topic/29425-sessions-help-again/
Share on other sites

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
Link to comment
https://forums.phpfreaks.com/topic/29425-sessions-help-again/#findComment-135034
Share on other sites

Thanks, Ken. I now have this at the top of my third page, [b]paystubresults.php:[/b]

[code]<?php
session_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).

Link to comment
https://forums.phpfreaks.com/topic/29425-sessions-help-again/#findComment-135043
Share on other sites

At the top of the script (after the session_start() statement), put the following:
[code]<?php
echo '<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
Link to comment
https://forums.phpfreaks.com/topic/29425-sessions-help-again/#findComment-135488
Share on other sites

[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]<?php
echo '<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]
Link to comment
https://forums.phpfreaks.com/topic/29425-sessions-help-again/#findComment-135547
Share on other sites

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 authenticate

Ray
Link to comment
https://forums.phpfreaks.com/topic/29425-sessions-help-again/#findComment-135618
Share on other sites

[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]<?php
session_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.
Link to comment
https://forums.phpfreaks.com/topic/29425-sessions-help-again/#findComment-135636
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.