473,397 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Including a function, and the variable created within cannot be found

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.

TIA
Jul 16 '05 #1
2 2324
Hi Keiron!

On Thu, 11 Sep 2003 20:12:55 +0000 (UTC), "Keiron Waites"
<webmaster@-NOSPAM-sharemonkey.com> wrote:
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.
Have a look at variable scopes in function on www.php.net, but better
look at functions and use return.

Jochen

TIA


--
Jochen Daum - CANS Ltd.
PHP DB Edit Toolkit -- PHP scripts for building
database editing interfaces.
http://sourceforge.net/projects/phpdbedittk/
Jul 16 '05 #2
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

Jul 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: pablo | last post by:
Dear Newsgroupers, The 'main' page contains a call to a function in an included file. This function puts a html-form on the screen. Before the form gets posted (to the 'main' page) some prior...
3
by: Jan-Henrik Grobe | last post by:
Hallo, I am coming to this newsgroup with a very strange Problem. I have two c++ files A.cpp and B.cpp....In A.cpp I am creating an openGL window and in B.cpp I have stored the callback...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
8
by: Jim Moon | last post by:
Hi. I'm curious about this syntax: <variable>=function(){...} I'm not finding a definition of this syntax, but I see it used at this website:...
7
by: Csaba Gabor | last post by:
I feel like it's the twilight zone here as several seemingly trivial questions are bugging me. The first of the following three lines is a syntax error, while the last one is the only one that...
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
1
by: INeedADip | last post by:
What is the difference between: function setupGrid( param ){......} and setupGrid = function( param ){......} Are there any advantages to doing one over the other?
4
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
1
by: mirandacascade | last post by:
1) Module1 has the following delcaration: Public g_frmZZZ as Form Public g_txtForm2 as Variant 2) app has two forms: form1 and form2 3) a command button on form1 opens form2; it also has...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.