474,064 Members | 2,987 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Brackets across includes

Hello all,

I am trying to create a user authentication system, and I would like to
separate the authentication code into include files. As I see it, the
basic flow would go something like this:

if (not authentic) {
display login
} else {
display content
}

I would like to separate this code so that the login bit is in an
included file. I imagined the breakup like this:

file: [auth_head.php]
-----------------------
if (not authentic) {
display login
} else {
-----------------------

file: [auth_foot.php]
-----------------------
}
-----------------------

So in each file which requires authentication, I can simply include the
first bit in the head, the second bit at the bottom, and put the
content in the middle. Makes sense, right?

Unfortunately, it seems that you cannot continue a { bracket }
statement across includes in this manner, as php errors. (It wants you
to wrap up your brackets before the end of the file.)

Does anyone know of a solution to this problem, or perhaps a
work-around?

Thanks very much, in advance.

-- Whit Nelson

Aug 24 '06 #1
5 1413
Rik
pe**********@gm ail.com wrote:
Hello all,

I am trying to create a user authentication system, and I would like
to separate the authentication code into include files. As I see it,
the basic flow would go something like this:

if (not authentic) {
display login
} else {
display content
}

I would like to separate this code so that the login bit is in an
included file. I imagined the breakup like this:

file: [auth_head.php]
-----------------------
if (not authentic) {
display login
} else {
-----------------------

file: [auth_foot.php]
-----------------------
}
-----------------------

So in each file which requires authentication, I can simply include
the first bit in the head, the second bit at the bottom, and put the
content in the middle. Makes sense, right?

Unfortunately, it seems that you cannot continue a { bracket }
statement across includes in this manner, as php errors. (It wants you
to wrap up your brackets before the end of the file.)

Does anyone know of a solution to this problem, or perhaps a
work-around?
In cases users not logged in may not see the whole page, I just:

if(not authentic){
header('HTTP/1.0 401 Unauthorized');
/* or alternatively you could redirect to the/a login page :
header('Locatio n: http://www.example.com/login.php'); */
exit;
}
//rest of code, which will not be displayed or run. No need for brackets

Grtz,
--
Rik Wasmus
Aug 24 '06 #2
*** pe**********@gm ail.com escribió/wrote (24 Aug 2006 11:29:57 -0700):
Does anyone know of a solution to this problem, or perhaps a
work-around?
Functions.
--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor con rayos UVA: http://www.demogracia.com
--
Aug 24 '06 #3
pe**********@gm ail.com wrote:
Hello all,

I am trying to create a user authentication system, and I would like to
separate the authentication code into include files. As I see it, the
basic flow would go something like this:

if (not authentic) {
display login
} else {
display content
}

I would like to separate this code so that the login bit is in an
included file. I imagined the breakup like this:

file: [auth_head.php]
-----------------------
if (not authentic) {
display login
} else {
-----------------------

file: [auth_foot.php]
-----------------------
}
-----------------------
It's ugly, it's messy and it's unmaintainable. Even if you could do it, its
not a good idea. You should apply the same discipline to any HTML fragments
you put in include files - opening and closing tags should match.

A better way of doing this is:

if (! $authenticated( )) {
require_once("l ogin.inc");
} else {
require_once("c ontent.inc");
}

There's a lot of reasons for all the changes to your code in the above.

HTH

C.
Aug 24 '06 #4
pe**********@gm ail.com wrote:
Hello all,

I am trying to create a user authentication system, and I would like to
separate the authentication code into include files. As I see it, the
basic flow would go something like this:
Before you start coding, try to get your concepts straight first.
Authentication determines the identity of the user. Authorization
determines if the user has permission to do something. These are
separate concepts and should be implemented as separate procedures.

When you restrict access to a page, the question you ask is "Is this
visitor permitted to view this page?" "The visitor is Joe Black"
clearly isn't a sufficient answer. To answer the question, you might
need to know who the visitor is--or might not. Authorization could be
granted on the basis of IP address for instance, or it could be granted
based on an authorization token received from a trusted source. It
could even be based on the time of day (e.g. "visitors are allowed from
8am-6pm").

To reiterate: identity =/=permission. Too many people get this wrong.

As Alvaro noted, you should use functions. Performing tasks by
including files is a primitive, stupid way to program. Include files
should only contain function/class declarations that're used by the
actual, executable script.

Here's an example of an authorization scheme. At the top of every page,
you would have something like this:

<?php

require('global .php');

CheckAuthorizat ion(0);

/* do stuff */

?>

Again, the question we're asking is "Is the visitor permitted to view
this page?". It's a authorization question--not authentication--hence
the function name. The parameter is the authority required for a
particular page. Here we'll use a simple numeric system. Zero authority
means the page is unrestricted. The function call might seem redundant
for this case, but it's useful to have the system cover the whole site.
If in the future you need to completely deny access to, say, a
particular IP address, the hooks are in place already.

The CheckAuthorizat ion function would look something like this:

<?php

function CheckAuthorizat ion($required_l evel) {
$visitor_level = GetVisitorPermi ssionLevel();
if($visitor_lev el < $required_level ) {
header("Locatio n: login.php");
exit(0);
}
return true;
}

function GetVisitorPermi ssionLevel() {
if(isset($_SESS ION['visitor_permis sion_level'])) {
return $_SESSION['visitor_permis sion_level'];
}
return 0;
}

function AuthorizeVisito r($level_grante d) {
$_SESSION['visitor_permis sion_level'] = $level_granted;
}

?>

The logic is fairly simple: If the visitor doesn't have the necessary
authority, then he's send to a login page. During the login process,
AuthorizeVisito r() would be called with a certain permission level,
perhaps retrieved from a database, once the visitor's identity is
acertained. The code might look something like this:

if(Authenticate User($_POST['login'], $_POST['password'])) {
$user_level = GetUserPermissi onLevel($_POST['login']);
AuthorizeVisito r($user_level);
}

The key, again, is that authentication is separate and distinct from
authorization. Keeping the distinction make the process clearer and
leaves options open for changes down the line. For instance, it'd be
relatively straight forward to extend the example above to support a
second method of authentication (e.g. HTTP).

Aug 24 '06 #5
pe**********@gm ail.com wrote:
Hello all,

I am trying to create a user authentication system, and I would like to
separate the authentication code into include files. As I see it, the
basic flow would go something like this:

if (not authentic) {
display login
} else {
display content
}

I would like to separate this code so that the login bit is in an
included file. I imagined the breakup like this:

file: [auth_head.php]
-----------------------
if (not authentic) {
display login
} else {
-----------------------

file: [auth_foot.php]
-----------------------
}
-----------------------

So in each file which requires authentication, I can simply include the
first bit in the head, the second bit at the bottom, and put the
content in the middle. Makes sense, right?

Unfortunately, it seems that you cannot continue a { bracket }
statement across includes in this manner, as php errors. (It wants you
to wrap up your brackets before the end of the file.)

Does anyone know of a solution to this problem, or perhaps a
work-around?

Thanks very much, in advance.

-- Whit Nelson
Whit,

The easiest way I've found doesn't require functions or anything else.
It also allows you to have the authorization only those pages which
require it.

authorize.php:

<?php
if (!isset($_SESSI ON['loggedon']) || $_SESSION['loggedon != true']) {
header('/logon.php');
exit();
}
?>

This checks the $_SESSION['loggedon'] variable to see if it is set and
true. If so, the process allows the rest of the page to be displayed.
If not, it redirects the user to 'logon.php'.

Change the test as necessary for your system. Then just

include('author ize.php')

at the very start (before anything - even DOCTYPE or white space) of any
php file requiring authentication.

No function calls to fool with, no worries about mismatched braces, etc.
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Aug 25 '06 #6

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

Similar topics

1
1654
by: WebLurker | last post by:
I have a site www.mysite.com. I have 3 subdomains sub1.mysite.com, sub2.mysite.com and sub3.mysite.com. sub3.mysite.com is password protected. All pages end with .php (ie index.php) I use the following code: <?php include("inc23.inc"); ?>
1
4486
by: d.schulz81 | last post by:
Hi all, We have about 10 different domains that are linked very closely and we want to identify and keep track of every single user that surfs our websites by the use of sessions. The problem is how to keep track of the session ID across domains. - cookies don't work because not acepted by 40 % of or users and cookies don't work across domains
1
2459
by: Gary Robinson | last post by:
Hi, I know that hash functions are often platform-dependent for efficiency reasons. From what I understand, this includes Python's hash(), which I have read is not guaranteed to return the same result across platforms or even across Python versions. Can someone tell me whether an MD5 hash using Python's MD5 library IS guaranteed to return the same results for the same input string, across platforms and Python versions?
0
1226
by: Daniel | last post by:
Hi! I have several projects (approx 50) that includes basically the same files such as <atlcom.h> and <comdef.h>. The projects are set up to use a precompiled header wich includes these header files (<atlcom.h> and <comdef.h>) and that works fine. But since these files are used in all the projects and every project have a separate precompiled header these files get compiled a lot of times. Is it possible to do anything about this? Is it...
9
5332
by: McGeeky | last post by:
Is there a way to get a user control to remember its state across pages? I have a standard page layout I use with a header and footer as user controls. Each page uses the same layout by means of copy paste (I hear this will improve in ASP.Net 2 via master pages). When I navigate from one page to the next the header and footer user controls lose their state because they are effectively different instances of the user control. Is there...
2
2188
by: Kenneth Porter | last post by:
If you email your users links and wrap the links in angle brackets, beware users using Yahoo Mail. It includes the trailing angle bracket in the link, preventing it from working. (This was happening with my site's "forgotten password" system, so people couldn't reset their passwords.) As a workaround, you can have your code check for a trailing ">" and strip it before further processing. I submitted a bug report using Yahoo's generic bug...
0
1007
by: alan4cast | last post by:
I'm not a really new programmer, but I'm still working on learning all of the .net things that I should know. So when I came across this one, I started to dig into it so see if I could figure it out... Here's what I know about brackets - If they are used around a reserved word then that becomes a usable (escaped) name (i.e. Dim as Integer). If they are used around a type in a DIM or NEW type statement then they're redundant and actually go...
5
1476
by: alan4cast | last post by:
I posted this in the VB forum several days ago, and got no reply. Since it's specific to VB.Net I thought I'd try it here. I'm a relatively-experienced VB programmer, but I'm still working on learning all of the .net things that I should know. So when I came across this one, I started to dig into it so see if I could figure it out... Here's what I know about brackets - If they are used around a reserved word then that becomes a usable...
13
5748
by: cront | last post by:
I have a problem to work on: we will ask user to input anything and we will put that back onto the standard output with all set of brackets removed. We will not remove any single bracket e.g. INPUT: comp.lang.(c++) OUTPUT: comp.lang. INPUT: comp.lang.(c++
0
10608
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10406
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
12239
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
12166
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
11188
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7928
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6724
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6921
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4984
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.