Sessions help for a newbie?

Discussion in 'PHP' started by PHPClueless, Dec 7, 2006.

  1. #1
    Hi! Thanks in advance to anyone who can answer this. I have a session variable (empcode) that isn't getting across.

    A snippet from the first page where employees enter their username (empcode) and password (password), with HTML tags and other formatting removed:

    
    <form action="employeeinformationmenu.php" name="leave" method="post" onSubmit="return validateForm(leave);">
    
    Employee Number:
    <input name="empcode" type="text" onSubmit="return validateForm(leave);"/>
    
    Password:
    <input name="password" type="password" / onSubmit="return validateForm(leave);">
    
    <?php
    session_register("empcode");
    session_register("password");
    ?>
    Code (markup):
    This part works great. They enter their employee number and password.

    Page 2

    At the top:

    
    <?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'];
    header('Content-Type: image/jpeg');
    ?>
    Code (markup):
    This works fine, as well. It pulls the info it's supposed to pull.

    I want to take the username and password already *allegedly* in the session, and send them to the next page using:

    
    <form action="paystubresults.php" name="paystubs" method="post">
    <input type="submit"/ value="Paystubs">
    </form>
    
    Code (markup):
    Page 3

    At the top:

    <?php
    session_start();
    $empcode = $_SESSION['empcode'];
    $middle = $_SESSION['middle'];
    $firstname = $_SESSION['firstname'];
    $lastname = $_SESSION['lastname'];
    $leavehours = $_SESSION['leavehours'];
    $password = $_SESSION['password'];
    ?>
    <?php
    echo '<pre>' . print_r($_SESSION,true) . '</pre>';
    echo $password;
    ?>
    Code (markup):
    The above echo statements give me:

    So it carries the password over, but not the employee number (empcode).

    Anybody?
     
    PHPClueless, Dec 7, 2006 IP
  2. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #2
    session_register is a deprecated function.
    When you initialize the session variable names on page 1 try:

    $_SESSION[empcode'] = "";

    and so forth.

    When setting the variables on page 2, I would rewrite the code as follows;

    if( isset( $_POST['empcode'])) { $_SESSION['empcode'] = $_POST['empcode']; }
    else { echo "DEBUG: empcode not set"; }

    These things may or may not cure the problem, but it may allow you to begin to narrow down the cause.
     
    clancey, Dec 7, 2006 IP
  3. PHPClueless

    PHPClueless Guest

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks. Your code gives me:

    DEBUG: empcode not set

    The only problem is, why is it not being set while password is?
     
    PHPClueless, Dec 7, 2006 IP
  4. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #4
    Well, its never a grt idea to leave passwords inside the session var, but if it's necessary, I'd do it like this, I assume page one posts to page two, on page two I'd use

    
    foreach ($_POST as $k => $x)
    {
       if($key != "password" || $key != "whatever")
       {
       $_SESSION['$k'] = $x;
       }
    }
    
    PHP:
    also on page three instead of that blah = _session['blah'] nonsense, try to access the _session var directly.

    I dunno if that's any help
     
    krakjoe, Dec 7, 2006 IP
  5. PHPClueless

    PHPClueless Guest

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Do you mean like this?

    WHERE M2.[EMPNO] = $empcode and 
    M2.[MSSNO] = $password
    Code (markup):
    That crashes the query.

    Or is my syntax wrong?
     
    PHPClueless, Dec 7, 2006 IP
  6. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I am sorry it has taken me so long to rejoin this thread. I need to back up because your problem getting the value for empcode is not yet solved.

    When you run the script and it says: "DEBUG: empcode not set" -- this means that the value for empcode is not being passed from the form.

    From your form I guess you are running some kind of javascript to validate the form input or it is being run through yet another script. But, I cannot understand why you need to include the statement "onSubmit="return validateForm(leave);" with each of the form's input elements. Would this not be saved for a submit button? Is that in your actual page 1 code?

    In my forms, I validate user input in the PHP script and redisplay the form when there is an error, indicating where the problems are. This is likely more a question of taste, though it solves the problem of javascript being turned off.

    If you need to include that in each input element, why does it refer to the name of the form instead of the name of element?

    For clarity sake, you should put a space before the "/>" part of the input element and you should move the closing slash in the password part to the end.

    I suspect the value for empcode is being removed by the validateFrom function.
     
    clancey, Dec 7, 2006 IP