Jump to content

Losing session when redirecting


king arthur

Recommended Posts

I am building a site with a membership system. It uses sessions, with a variable $_SESSION["loggedin"] to indicate if the user is logged in or not, and this is used by the menu code to give access to the free pages if not logged in, and the member pages if logged in.

I have it working and have got a few members joining up but noticed from my stats that one or two didn't seem to be able to access the members pages after successfully logging in. The login page does a header redirect as soon as the user has correctly entered their details, and I found that if the browser is not accepting cookies, the session ID is not appended to the URL and therefore the session is lost. This may not be the whole problem but it is a problem.

Is there any way of getting PHP to add the session ID to the URL when doing a header redirect, when a cookie cannot be sent? Or should I just redesign my login page to work another way. Also is there any other reason why the session could be getting lost, as it didn't look like these members were actually blocking cookies. I have session_start() at the top of every page.
Link to comment
Share on other sites

Yes there is,

Use this instead,

[code]
function redirector($location) {
  $sname = session_name();
  $sid = session_id();

  if( strlen( $sid ) < 1 ) {
      Header( $location );
      return;
  }

  if( isset( $_COOKIE[ $sname ] ) || strpos( $location, $sname."=".$sid ) !== false ) {
      Header( $location );
      return;
  } else {
      if( strpos( $location, "?" ) > 0 )
          $separator = "&";
      else
          $separator = "?";

      $fixed = $location . $separator . $sname."=".$sid;
      Header( $fixed );
      return;
  }
}
[/code]
Link to comment
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.