Jump to content

Session inside a function.


Speedysnail6

Recommended Posts

Hi,

I'm not sure if either I just have had a stupid error or what. Here is my code on the page with the functions for my script, which is called using require_once() on my other page.

session_start();
function log_in($username, $password) {
	global $ss_con;
	$_SESSION['logged_in'] = 'true';
	$_SESSION['username'] = $username;
}

This is the code for the other page that has require once...

require_once('firstpage.php');
log_in();
if ($_SESSION['logged_in'] != 'true') {
	echo $_SESSION['logged_in'];
	echo 'fail!';
	}

The responce I get is "fail!"

 

Don't worry about log_in() being set. I did that on another page on the same host that just forwards to this.

Please help me!

Link to comment
https://forums.phpfreaks.com/topic/282851-session-inside-a-function/
Share on other sites

Try putting session_start at the very top of the page that requires your functions. Also, never, ever, ever use 'global'. For anything. You're already using the function's argument list. Just pass $ss_con in through it like you do with the other arguments.

I appreciate your help, but that isn't the problem. In the function page, BELOW the function log_in, I put

$_SESSION['logged_in'] = 'true';
$_SESSION['username'] = $username;

and it worked. It may be a problem with the log_in() being called, but I doubt it. Are you sure sessions can be set INSIDE a function?

Archived

This topic is now archived and is closed to further replies.

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