Jump to content

SESSION Variable Problem


danielc111

Recommended Posts

I'm using Session variables for the first time on a site I'm developing. I had it working fine while I was doing some admin and testing in subfolders. But the problem is I'm losing the session variables when I load the page from www.example.com, but it works from www.example.com/index.php. I would be happy to post some code if needed.

 

Link to comment
Share on other sites

Loading example.com/ and example.com/index.php should be the same, unless you have a "index.html" (typical in Apache installation: in DirectoryIndex, index.html is served first). Otherwise, session data should be available. Try posting some code.

Link to comment
Share on other sites

function validateUser($id){    
session_regenerate_id (); 
    $_SESSION['valid'] = 1;
$_SESSION['userid']=$id;
return true;
}

function isLoggedIn(){
    if($_SESSION['valid'])
return true;
return false;
}

 

So when the user logs in with a correct password, I pass their id to the validateUser function. This sets the variable. Then I is the isLoggedIn function to see if there is a session site while they are on the site.

 

I have no .html files on the server. I have some other php that works fine on example.com and example.com/index.php

Link to comment
Share on other sites

Try this on your after session

 

<?php
//Start session
session_start();

//Check whether the session variable SESS_MEMBER_ID is present or not
if(!isset($_SESSION['SESS_ID']) || (trim($_SESSION['SESS_ID']) == '')) {
	header("location: access-denied.php");
	exit();
}
?>

 

this should go on all the top of your pages.

Link to comment
Share on other sites

That works beautiful and simple enough to tell whether a user is logged in or not.

 

To take it one step further, since I am still losing my $_SESSION['userid'] variable on www.example.com, would it be good practice to find the user id by retrieving it from the mysql row that contains the $_SESSION['SESS_ID'] string vs. passing it around as a session variable?

 

Thank you for your help!

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.