473,386 Members | 1,758 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,386 software developers and data experts.

Suggestion for handling pages requiring redirect using Config File

I thought I had a workable approach to specifing which pages required a
redirect in a config file, but it appears the way I'm attempting to do
it is not going to work.

The idea is that I can specify in the config file all of the pages that
require a user to login otherwise the page will redirect if the user is
not logged in.

config.php looks like this:
----------------------
// This allows you to set which pages require a login. If the
// user tries to access a page specified below, they are
// redirected to the specified page (usually the
// registration page).

// The # of pages that require user login
$_CONF['required_login_pages'] = 1;
$_CONF['required_login_0'] = '/myaccount.php'; //(slash required)
----------------------

and header.php (which is included in every page) looks like this:
----------------------
$url = $HTTP_SERVER_VARS["REQUEST_URI"];
$numberPages = $_CONF['required_login_pages'];

for($i = 0; $i < $numberPages; $i++) {
print "page = " . $_CONF['required_login_$i'];
if(beginsWith($url, $_CONF['required_login_$i'])) {
//header("Location:".$_CONF['site_url']"./registration.php");
print $_CONF['required_login_$i'];
}
}

// returns true if $str begins with $sub
function beginsWith( $str, $sub ) {
return ( substr( $str, 0, strlen( $sub ) ) == $sub );
}
---------------------

The problem is php doesn't seem to like the $_CONF['required_login_$i']
statement in the for loop. Anyone have a suggestion on another option
for handling this?

Thanks, Greg

Nov 22 '05 #1
4 1756
Greg Scharlemann said the following on 12/11/2005 23:01:
I thought I had a workable approach to specifing which pages required a
redirect in a config file, but it appears the way I'm attempting to do
it is not going to work.

The idea is that I can specify in the config file all of the pages that
require a user to login otherwise the page will redirect if the user is
not logged in.

config.php looks like this:
----------------------
// This allows you to set which pages require a login. If the
// user tries to access a page specified below, they are
// redirected to the specified page (usually the
// registration page).

// The # of pages that require user login
$_CONF['required_login_pages'] = 1;
$_CONF['required_login_0'] = '/myaccount.php'; //(slash required)
----------------------


Urrgghh, nasty!
Use nested arrays instead, i.e.:

$_CONF['required_login'][0] = '/myaccount.php';
$_CONF['required_login'][1] = '/other.php';
$_CONF['required_login'][2] = '/another.php';
....

That way, you don't even need $_CONF['required_login_pages'], as
count($_CONF['required_login']) will give the same effect.

You could then reduce your test logic to:

if (array_search($_SERVER['PHP_SELF'], $_CONF['required_login']))
{
header("Location: ...");
exit();
}

(Not tested)

--
Oli
Nov 22 '05 #2
Greg Scharlemann wrote:
I thought I had a workable approach to specifing which pages required a
redirect in a config file, but it appears the way I'm attempting to do
it is not going to work.

The idea is that I can specify in the config file all of the pages that
require a user to login otherwise the page will redirect if the user is
not logged in.


That's not an approach I'd favor for practical reason. While you're
working in your config file, it's hard to remember exactly and fully
which pages require login and which do not. Personally I prefer to let
the page make the determination individually, as the context is clear
while you're working in it.

What I usually do is have a function like this:

function RestrictAccess() {
if(!GetUserID()) {
Redirect("login.php");
}
}

And then call it on any page that isn't public. For an application with
multi-level access, the function would take a parameter, indication
what level is required.

Nov 22 '05 #3
You need to use the eval statement in order to use a variable in a
variable name, something like:
eval("print \$_CONF['required_login_$i'];");

But I agree with the other commenters. Let the page determine this. I
usually put an include statement at the top of each file needing it.
The included code checks if the session login variable is set and if
not, uses a header statement to relocate to the login page.

Kitty
http://OpenSkyWebDesign.com

Nov 22 '05 #4
Following on from Greg Scharlemann's message. . .
I thought I had a workable approach to specifing which pages required a
redirect in a config file, but it appears the way I'm attempting to do
it is not going to work.


Bad approach in general. Horrible to maintain and as I understand your
logic a page not in the list gets away scot-free and so fails unsafe.

The better approach is to have a 'should I be here' function at the top
of any page where you want any control over 'should I be here'.
Obviously this goes into a single call of a function with all the other
standard top of page stuff.

You may notice the test is 'should I be here' not 'should we jump
elsewhere' that is focussing on the reasons that drive the logic rather
than the results. There are all sorts of reasons for jumping pages
elsewhere:-
* Not logged in
* Insufficient permission
* You have arrived at a record detail page without going through a
'select record' screen.
* You have arrived at a screen out of sequence where the sequence is
important
* News flash
* You have finished an excursion (eg a generic pick a date screen called
from anywhere) and want to return to where you left off.

Also, and 'hey you must login first - here we go' is the classic
example, you might be wanting to handle the situation in-line as far as
your page logic is concerned. ie.
o Test-Jump elsewhere-Continue
o Test-Jump elsewhere (then come back here)-Continue.
o Test-Set flags-Continue (constrained by flags)

So for example in a menu we might test to see if the user has
administrator's rights and if so switch on additional menu items.
Although this doesn't involve a jump it is handy to be able to class it
together with those sort of tests at the top of the page and then all
that sort of stuff has been got out of the way and anyone coming to look
at a the code can quickly grasp what it is about.

I implement a HaveWeComeFrom function which takes a list of possible
pages we might have just left as an argument (only where it is important
of course) which if it fails goes back to a configured main menu page
and 'can't do that there 'ere' message.

I hope this ramble through checking logic illustrates why it's best to
put the test parameters in the pages themselves.
--
PETER FOX Not the same since the bottom fell out of the bucket business
pe******@eminent.demon.co.uk.not.this.bit.no.html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.demon.co.uk>
Nov 22 '05 #5

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

Similar topics

3
by: CoreyMas | last post by:
This is what i am trying to do I have a DAL that contains all of my database connections/reads etc. I have a separate GUI layer that has my web pages I would like to pass my exception errors...
2
by: Kenneth Keeley | last post by:
Hi I have created a Web.config file with theses settings: <customErrors mode="RemoteOnly" defaultRedirect="/errorpages/generic.aspx"> <error statusCode="403" redirect="/errorpages/noaccess.aspx"...
14
by: Al Smith | last post by:
I need help in implementing proper error handling. I am trying to upload a file based on the sample code below. The code works well except if the file selected is too big. I do know about the...
2
by: Eric | last post by:
I am trying to build an app where the stuff in the root directory is open to all, but anything under the Restricted directory requires you to login and I want to use Forms to do it. I'm having...
0
by: Joost | last post by:
Hello all, I am currently developping an intranet website for my company and I encountered a strange thing. I have a global error handler (application_error) in my global.asax file which...
4
by: Jarrod | last post by:
Hallo, Have just created a small ASP .Net App with VS 2003 for the first time in years. After Building a release I proceed and uploaded my folder onto the wwwroot. The aspx pages just throw an...
2
by: the friendly display name | last post by:
Hello, following problem: I have set two folders in the web.config file, that should be protected against anonymous users. I am using forms authentification. Is there a way to use two login...
2
by: tshad | last post by:
This has been driving me crazy. I have been trying to get the error handling working on my system and can get parts of it working and others won't work at all. I found that you can't access...
35
by: jeffc226 | last post by:
I'm interested in an idiom for handling errors in functions without using traditional nested ifs, because I think that can be very awkward and difficult to maintain, when the number of error checks...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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:
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...

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.