N
N
nikolaypetrov2015-03-05 21:26:48
PHP
nikolaypetrov, 2015-03-05 21:26:48

How to block access to a script in PHP?

There is a php script.

Can be launched via include, can receive requests via ajax.

How to prevent access to it via a direct link? Is it possible somehow without htaccess?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
S
Sergei Makhlenko, 2016-12-08
@weblive

Whoa whoa. In index.php, create a constant that you will check, and then check it in all included files.

// index.php - до подключения других файлов
define('MY_CONSTANT', TRUE);

// во всех подключаемых файлах делайте проверку
<?php
if (!defined('MY_CONSTANT')) exit('No direct script access allowed.');

class MY_Class {
....
}

ps did not look at the date of the question)) but it may be useful

F
FanatPHP, 2015-03-05
@FanatPHP

Leave this file alone. Nobody needs him.
It is much more important for you to understand that
1. Your question has nothing to do with information security
2. It is basically impossible to "close" this file from outside access.
3. All of the above plus your favorite spell "xtaxess! xtaxess!" tells us that you don't understand how the browser-server system works. And this is something a hundred times more important than your unfortunate file. You need to understand how a web application works, where you have PHP, and where ... Once you understand this, you will no longer have stupid questions like this.

A
Andrey Mokhov, 2015-03-06
@mokhovcom

if (preg_match('/' . preg_quote($_SERVER['PHP_SELF'], '/') . '$/i', str_replace('\\', '/', __FILE__))) {
    exit();
}

M
Maxim Gavrilov, 2015-03-07
@thestump

chmod 700 filename

O
Oleg, 2015-03-08
@ollisso

If you paraphrase the question like this: how to give the opportunity to open a file only with the help of Ajax, but not give it through a direct link, then the answer is simple:
Check that this is an Ajax request and if it is not, then exit.
Check example:

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
  // аякс
}else{ 
// не аякс
}

Of course, this is not protection against hackers and so on. Only protection against direct opening in the browser
Anyone can add such a header.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question