/    Sign up×
Community /Pin to ProfileBookmark

ordering includes, functions, and headers

i’m just about to pull my hair out over this one. here’s the basic flow of my login.php page right now

[code]
require(header.php)//contains the login function and the beginning of the html

if(login(user, pass)){
header(“Location: somewhere.php”);
}

require(footer.php)//contains the end of the html
[/code]

problem, obviously, is that i can’t go somewhere else after html has started, right? so i would put all the php stuff above the header include, but then i wouldn’t be able to get at the login function. though, strangely, php does seem to be able to call functions that are extablished later in the script sometimes, but is this a safe thing to do? will it work in most versions of php?
is there another way to change the page without the visitor having to click anything, but that can be done from the middle of the page? last resort option is, of course, to split up the header into 2 files, but i would rather not do that. can i depend on the logout function working if i call it before it’s defined?

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 19.2006 — For PHP4 and PHP5, a function can be defined anywhere (the exception being conditional function definitions, which I doubt you need to worry about here).

Anyway, what I've found to be useful to do is for common HTML output that I define in an include file, I put it into a function definition. Then I include that file in the main file, and call the function where needed:
[code=php]
<?php
require_once "include_file.php"; // includes top() function definition
if(some_condition_is_true)
{
header("Location: somewhere.else");
exit;
}
top(); // outputs common top-of-page HTML
// etc....
?>
[/code]
Copy linkTweet thisAlerts:
@gameguy43authorJun 19.2006 — Genius!

edit: but i have php in my header, so how do i echo the html and evaluate the php?
Copy linkTweet thisAlerts:
@gameguy43authorJun 20.2006 — ok, so its all good except i realise the problem is that i cant access variables that exist outside the function, but i want to be able to. is there a way to just tell the function "you can access variables outside this function", or do i have to make all of these variables global?
Copy linkTweet thisAlerts:
@NogDogJun 20.2006 — Either define them as "global" in your function, or use the $GLOBALS array (see Example 12-3 at http://www.php.net/manual/en/language.variables.scope.php ). Howver, using global variables has a number of potential pitfalls, so usually it is better to make them into function parameters if at all possible.
Copy linkTweet thisAlerts:
@NogDogJun 20.2006 — PS: Alternatively, you may be looking at a good case for defining a class with attribute variables which would be your "global" values and multiple methods which would be the various functions that interact with those attributes. ?
Copy linkTweet thisAlerts:
@gameguy43authorJun 20.2006 — hmmm. that class thing seems incredibly confusing. if there are security issues with globals, i'd better not use them because if they could change one of these variables, they'd get free access to secure pages. i suppose i could pass the variables i need to the function, but it might be a somewhat long list of arguments, and i want to be able to easily expand the script. but i'm all about good programming practices, so i'll pass them all (its not THAT long of a list?. one more quick question, are defined constants, as defined with define(), global by default? maybe the best solution is to set all these commonly used variables as constants?
Copy linkTweet thisAlerts:
@gameguy43authorJun 20.2006 — thats what i did, and i suppose that's the reason why the define() function exists. i suppose as a good rule of thumb, if the value is just going to be specified once and never mannipulated, it might be best off as a constant.
×

Success!

Help @gameguy43 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...