header() redirection/session variable problem

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default header() redirection/session variable problem

    Hello,

    I'm having an odd problem with combining an authentication session
    variable with header() redirection. Basically I have an authentication
    script which checks a username/password. If the login is correct a
    session variable containing the username is set and the user is
    redirected to the (restricted access) main menu page.

    The problem is, the FIRST (and only the first) time the user attempts
    to login, the "Session variable not set" error message comes up when
    the user is redirected to the private page. However the SECOND attempt
    to login works.

    Can anyone shed some light on this? The relevant code is below; system
    is PHP4.3.1 on IIS, 5 I think. The error didn't occur when the PHP was
    4.2.x - is this a known 4.3 issue?

    Thanks,
    Nick

    //////////////////////// Login/authentication script
    ////////////////////////

    // Code to check for correct password snipped

    // Save the username in a session variable. This will be used in all
    // private pages.
    $_SESSION['username'] = $username;

    // Tried this to attempt to solve the problem. It didn't work.
    // sleep(1);

    // Redirect to private page

    mysql_close($conn);
    header("Location: main.php?page=0");

    //////////////////// Private main index page
    ///////////////////////////////////

    // Check the username session variable exists
    if (isset($_SESSION['username']))
    {
    // Write out private page
    }
    else
    {
    echo "Session variable not set!<A HREF=login.html>Back to
    login</A>";
    exit;
    }
    Nick Whitelegg Guest

  2. Similar Questions and Discussions

    1. session variable increment problem
      session_start(); $_SESSION++; print 'You have visited here '.$_SESSION.' times.'; why the visit variable is not incremented the next time i...
    2. session variable problem
      I have a Session object set on Session_Start... I figured out that the object is never created... Cannot understand why.. Also I noticed that...
    3. Session Variable problem with CFLOCK
      Hi Guys here is my error message: Cannot lock Session scope.CFLock cannot be used to lock the application or session shared scopes without...
    4. Session and Server variable problem
      Hi all, Have an interesting problem. Worked on a web site for a client. They have the server (IIS) set up and we are using php. Have been...
    5. some kind of redirection (not the header function)
      Hi!!! I´m programming a site in PHP, where I want to do the following: I will have a set of different directories, where each directory must...
  3. #2

    Default Re: header() redirection/session variable problem

    In article <c12cf038.0309171127.3ca4290f@posting.google.com >,
    [email]nick.whitelegg@solent.ac.uk[/email] (Nick Whitelegg) wrote:
    > The problem is, the FIRST (and only the first) time the user attempts
    > to login, the "Session variable not set" error message comes up when
    > the user is redirected to the private page. However the SECOND attempt
    > to login works.
    You have to initialize or re-open a session with a call to
    session_start(); before you start working with $_SESSION variables.

    There was no session_start() in your code examples. It might have been
    snipped, of course, but that is the first thing that comes to mind.

    JP

    --
    Sorry, <devnull@cauce.org> is een "spam trap".
    E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
    Jan Pieter Kunst Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139