Jump to content

protect a file


fxuser

Recommended Posts

In my javascript code i use ajax so i load a php file .. then on the success i have a function where i load a file there.. the thing is how can i secure it from direct access from the browser?

 

if i type the link of the file in the browser i can access it even tho it needs other files to run so it will return me errors.. how can i disable that?

 

Thanks.

 

EDIT: i have tried to put

if(!defined("MAIN")){
die('<tt>You cannot view this file directly!</tt>');
}

 

on the file i want to protect but then when i call the ajax on the success the file doesnt load on the page that i call it and shows the above die();

Link to comment
Share on other sites

Thanks for the reply phpchamps,ill try it , wouldnt a htaccess with Deny from all do the job aswell?

 

EDIT : it doesnt allow me to direct check it with your code .. which is the same as mine with the define("main","true");

 

but when i go to ajax and press the button i get this: You cannot view this file directly! and doesnt allow me to load the file i want when it success

Link to comment
Share on other sites

define("main","true"); is not the same as define("MAIN","true"); unless you set the 3rd parameter to TRUE.

 

Perhaps if you posted the actual code you put into the main file to define the constant and the code you put into the include file to check if the constant is defined.

Link to comment
Share on other sites

i tried defining a var in the file that i include the included file and checking if the defined var exists in the included file but that does not work.. also i tried phpchamps code above .. i putted it to a php file that i include and call it on the top of the included file where also ajax tells me that i cant load the file when on ajax success

Link to comment
Share on other sites

please post the full code.

 

this is how the file that loads the included file is:

<?php
define("MAIN", "True");
include_once 'config.php';

$status = "0";
$id = sanitizeString($_GET['id']); //e.x ?id=user1 we get user1

 

this is how my included file starts:

<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/config.php';
check_un($_GET['id']);
$_GET['p'] = preg_replace('#[^0-9]#i', '', $_GET['p']);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.