Keiron Waites writes:
Hi,
I include the following function:
<?php
function login($members_only) {
if (isset($_COOKIE['login'])) {
$login = explode("|split|",$_COOKIE['login']);
$login = array("username"=>$login[0],"password"=>$login[1]);
}
else if ($members_only == 1) {
header("Location: login.php");
}
}
?>
in a file called account.php:
<?php
require("website-includes/login.inc.php");
login(1);
[snip]
?>
The problem is that I need to be able to access $login outside of the
function within the page that calls it. Is this possible? I'm new to
functions and before I was just including code, but I am told functions are
better.
At the end of your function you could add :
return $login;
and call it with:
$login = login(1);
Another thing you could is use objects to define a scope in which
functions can share variables. Like this:
<?php
class login_class {
// Shared variables used by login functions.
var $login;
var $members_only;
// Constructor function that intialises the object.
function login_class( $members_only ) {
$this->members_only = $members_only;
}
// The login function now refers to class variables.
function login() {
if (isset($_COOKIE['login'])) {
$this->login = explode("|split|",$_COOKIE['login']);
$this->login = array("username"=>$this->login[0],
"password"=>$this->login[1]);
}
else if ($this->members_only == 1) {
header("Location: login.php");
}
}
// Another function that uses $this->login
function get_username() {
return $this->login("username");
}
}
?>
<?php
require("website-includes/login_class.inc.php");
$login_class = new login_class(1); // Instantiate and initialise your class.
$login_class->login(); // Call a function.
?>
--
__o Alex Farran
_`\<,_ Analyst / Programmer
(_)/ (_)
www.alexfarran.com