Forum Moderators: coopster

Message Too Old, No Replies

restrict direct access

         

yllai

7:59 am on May 4, 2005 (gmt 0)

10+ Year Member



hi,

when user goto my page, they have to login with username and password, then they can access all link in my page. How can I restrict user from direct access to my content pages which need user login before access it?

Any idea?

anshul

11:46 am on May 4, 2005 (gmt 0)

10+ Year Member



HTTP Authentication ( by Apache ) is best/easiest/secure option. Or try cookie or session variables. Or use .htaccess file.
There are very easy examples in PHP Manual ( see related PHP functions ). I hope, you've already downloaded that. Or search Google.

jatar_k

4:58 pm on May 4, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try this
[webmasterworld.com...]

Stormfx

8:45 pm on May 4, 2005 (gmt 0)

10+ Year Member



I think jatar_k's solution is what you're referring to, but to prevent included files from be loaded independantly, use something like this:

In a public page:

define('ACCESS',true);

In included files, use:

if (!defined('ACCESS')) {
exit;
}

henry0

8:58 pm on May 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Start a session and include per each page your authentication script ()
and do a {if!auth ... back to login}else encompass your page content in between the else { }
Of course I took many shortcuts but you have the basics

by doing so even if a page URL is bookmarked and called it won't work if not authenticated properly

yllai

12:51 am on May 5, 2005 (gmt 0)

10+ Year Member



is it if I add below script to begining of my page will be another solution for it?

if (!eregi("modules$PHP_SELF)) {
die ("You can't access this file directly...");

yllai

12:52 am on May 5, 2005 (gmt 0)

10+ Year Member



sorry, typing error. should be:

if (!eregi("modules.php", $PHP_SELF)) {
die ("You can't access this file directly...");

Stormfx

4:36 am on May 5, 2005 (gmt 0)

10+ Year Member



That could work, but regex functions are pretty slow. Why not just do what I mentioned above?

First line of all your accessible pages:


define('ACCESS',true);

First line of your protected pages (inlcudes):


if (!defined('ACCESS')) {
exit;
}