473,412 Members | 2,281 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,412 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 2325
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: 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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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:
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.