How to prevent 403, 400 errors URL forwarding? - php

Site works good when user enter any url which doesn't exist and forward request to error controller.
But
If you write a script in url site throw 403 error
If you write some asp codes site throw 400 error
How can i prevent this and forward them to custom 403 and 404 pages? I tried forward with htaccess but i couldn't succeed it.
Another problem:
If you write ANY ascii code in adress bar, bootstrap forward to welcome page (controller ->index.php) .How is possible?
Directory structures, htaccess and bootstrap codes are below. Thank you for any help.
Directory structure:
/config
/libs
-bootstrap.php
/controllers
/models
/views
/public_html
-index.php
htaccess
htaccess
htaccess in main directory
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public_html/ [L]
RewriteRule (.*) public_html/$1 [L]
</IfModule>
htaccess in public_html directory
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
index.php in public_html
<?php
define('DS', DIRECTORY_SEPARATOR);
define('ROOT', dirname(dirname(__FILE__)));
require_once (ROOT . DS . 'libs' . DS . 'bootstrap.php');
$app = new Bootstrap();
bootstrap.php in libs
<?php
class Bootstrap {
function __construct() {
$url = isset($_GET['url']) ? $_GET['url'] : null;
$url = rtrim($url, '/');
$url = explode('/', $url);
print_r($url);
if (empty($url[0])) {
require '../controllers/index.php';
$controller = new Index();
$controller->index();
return false;
}
$file = '../controllers/' . $url[0] . '.php';
if (file_exists($file)) {
require $file;
} else {
$this->error();
return false;
}
$controller = new $url[0];
// calling methods
if (isset($url[2])) {
if (method_exists($controller, $url[1])) {
$controller->{$url[1]}($url[2]);
} else {
$this->error();
}
} else {
if (isset($url[1])) {
if (method_exists($controller, $url[1])) {
$controller->{$url[1]}();
} else {
$this->error();
}
} else {
$controller->index();
}
}
}
function error() {
require '../controllers/error.php';
$controller = new Error();
$controller->index();
return false;
}
}

Related

Error 403, access denied on infinityfree domain hosting

I'm currently testing my project which has MVC integrated in it upon deploying in a free hosting site i've encountered an error 403 this error didn't appear since the development stage. My current knowledge why this error occurs is maybe the type of directives i've used in my htaccess? or something maybe in the core itself any toughts or suggestions to this particular problem :)
domain/
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
domain/public
<IfModule mod_rewrite.c>
Options -Multiviews
RewriteEngine On
RewriteBase /domain/public
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
</IfModule>
domain/app
Options -Indexes
domain/app/libraries/Core.php
<?php
class Core {
protected $currentController = 'Pages';
protected $currentMethod = 'index';
protected $params = [];
public function __construct(){
//print_r($this->getUrl());
$url = $this->getUrl();
// Look in BLL for first value\
if($url != NULL){
if(file_exists('../app/controllers/' . ucwords($url[0]). '.php')){
// If exists, set as controller
$this->currentController = ucwords($url[0]);
// Unset 0 Index
unset($url[0]);
}
}
// Require the controller
require_once '../app/controllers/'. $this->currentController . '.php';
// Instantiate controller class
$this->currentController = new $this->currentController;
// Check for second part of url
if(isset($url[1])){
// Check to see if method exists in controller
if(method_exists($this->currentController, $url[1])){
$this->currentMethod = $url[1];
// Unset 1 index
unset($url[1]);
}
}
// Get params
$this->params = $url ? array_values($url) : [];
// Call a callback with array of params
call_user_func_array([$this->currentController, $this->currentMethod], $this->params);
}
public function getUrl(){
if(isset($_GET['url'])){
$url = rtrim($_GET['url'], '/');
$url = filter_var($url, FILTER_SANITIZE_URL);
$url = explode('/', $url);
return $url;
}
}
}

AltoRouter never match subfolder

I tried to use the AltoRouter on my webspace in a subfolder. But the matches always false!
I have this structure on my webspace
domain.com
--/2 --> subfolder
----index.php
----.htacces
My .htaccess file looks like this:
DirectoryIndex index.php
RewriteEngine on
RewriteBase /2/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
So I setup up the router like this:
<?php
session_start();
require_once 'vendor/autoload.php';
use LemWebPortal\Config;
$latte = new Latte\Engine;
$router = new AltoRouter();
$router->setBasePath('/2/');
$router->map('GET', '/', function () {
require __DIR__ . '/src/Init/index.php';
});
$router->map('GET', '/schulungsbedarf', function () {
require __DIR__ . '/src/Init/courseRequest.php';
});
$match = $router->match();
if ($match && is_callable($match['target'])) {
call_user_func_array($match['target'], $match['params']);
} else {
// no route was matched
header($_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
}
echo '<pre>';
var_dump($match);
Now, when I call domain.com/2/or domain.com/2/schulungsbedarfI see an empty white page with this message:
bool(false)
What did I miss here?

PHP site run correct on the localhost:8000 but gives erreurs on XAMPP or other server. How to create .htaccess?

This is my first experience to create a site in PHP.
My project run correct on the php interne server , but not on the Xampp ni on the server ovh
ovh serveur erreur: App\Router::run(): Failed opening required '/home/myla/www/views/.php' (include_path='.:/usr/share/php': var $view false
localhost:8000 work correct: var $view post/index
XAMPP erreur : Trying to access array offset on value of type bool :
var $view false
The probleme is in the var $view in the function run().
public function run ():self
{
$match = $this->router->match();
$view= $match['target'] ;
$params= $match['params'];
$router= $this;
$isAdmin= strpos($view, 'admin/') !== false;
$isUser =strpos($view, 'user/') !== false;
if(!$isAdmin && !$isUser){
$layout = 'layouts/default';
}
if($isUser){
$layout = 'user/layouts/default';
}
if ($isAdmin) {
$layout = 'admin/layouts/default';
}
try{
ob_start();
require $this->viewPath . DIRECTORY_SEPARATOR . $view . '.php';
$content = ob_get_clean();
require $this->viewPath . DIRECTORY_SEPARATOR . $layout . '.php';
} catch (ForbiddenException $e) {
header('Location: ' . $this->url('login') . '?forbidden=1');
die();
}
return $this;
}
I use run() in the index.php :
$router = new App\Router(dirname(__DIR__) . '/views');
$router
->get('/', 'post/index', 'home')
->get('/blog/category/[*:slug]-[i:id]', 'category/show', 'category')
->get('/blog/[*:slug]-[i:id]', 'post/show', 'post')
->match('/login','auth/login','login')
->match('/register','auth/register','register')
->post('/logout','auth/logout','logout')
->run();
I try to create .htaccess to re-write the rule, but i didnt found the good configuration.
File index.php is in the folder public and folders public,src,views are in the racine of the projet .
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /public/index.php [L]
Its not work.
Can you kindly help me to configurate .htaccess
Thanks en advance.
Here is a good configuration:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]

Php mvc not loading images using <img>

I have made my own php MVC and it works perfectly for everything except when it comes to loading certain images. For example it will load like icons, and some other images but when it comes to using the tag it wont load the image but when i look in the google chrome developer inspector tool it shows that my tag images aren't loading. Its giving back a 301 number and loading the HTML. So in terms its like my MVC is thinking the URL for the image is a page!I want my MVC to only make requests to my index.php file which is in the root directory for my site. Also when it comes to it generating the HTML code i mean that it going to my error html page when i put the direct image url
in the browser.
Absolute URL for the image:
http://localhost/website/user_data/cfedabd2e283c19c06f3b8b5bd45df4bb66c2b3a/photo.jpg
Here is my code below:
.htaccess--
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?u=$1 [PT,L]
</IfModule>
bootstrap.php(Router)--
class Bootstrap {
function __construct() {
$url = $_GET['u'];
$url = rtrim($url, '/');
$url = explode('/', $url);
if (empty($url[0])) {
require 'controllers/index.php';
$controller = new Index();
$controller->index();
return false;
}
$file = 'controllers/' . $url[0] . '.php';
if (file_exists($file)) {
require $file;
} else {
$this->error();
}
$controller = new $url[0];
$controller->loadModel($url[0]);
// calling methods
if (isset($url[2])) {
if (method_exists($controller, $url[1])) {
$controller->{$url[1]}($url[2]);
} else {
$this->error();
}
} else {
if (isset($url[1])) {
if (method_exists($controller, $url[1])) {
$controller->{$url[1]}();
} else {
$this->error();
}
} else {
$controller->index();
}
}
}
function error() {
header('location: ' . APP_URL . 'index');
}
index.php(Where i want all the requests to go)--
<?php
//error_reporting(E_ALL);
session_start();
/* Require all config files */
require 'config/config.php';
/* Initiate system */
$bootstrap = new Bootstrap;
?>
Example What the user sees--
<h3>Error page not found</h3>
The image with
$photo = "http://localhost/website/user_data/cfedabd2e283c19c06f3b8b5bd45df4bb66c2b3a/photo.jpg;
<img src="<?php echo $photo; ?>" width="200" style="max-height: 200;"/>

404 Page Not Found The page you requested was not found.- CodeIgniter

Hello guys i try to working on CodeIgniter 1.7.1 i have done step to step installing
but its showing Error
404 Page Not Found
The page you requested was not found.
here is codes i used
controller home.php
class Home extends Controller {
function __construct()
{
parent:: __construct();
//$this->output->enable_profiler();
$this->load->library('DX_Auth');
}
Models homemodel.php
class HomeModel extends Model {
function __construct()
{
// Call the Model constructor
parent::Model();
}
.htaccess
RewriteCond $1 !^(index\.php|assets|xcache|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
i using localhost "XAMPP"
please help me
and main index.php
<?php
error_reporting(E_ALL);
$system_folder = "system";
$application_folder = "application";
if (strpos($system_folder, '/') === FALSE)
{
if (function_exists('realpath') AND #realpath(dirname(__FILE__)) !== FALSE)
{
$system_folder = realpath(dirname(__FILE__)).'/'.$system_folder;
}
}
else
{
// Swap directory separators to Unix style for consistency
$system_folder = str_replace("\\", "/", $system_folder);
}
define('EXT', '.'.pathinfo(__FILE__, PATHINFO_EXTENSION));
define('FCPATH', __FILE__);
define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME));
define('BASEPATH', $system_folder.'/');
if (is_dir($application_folder))
{
define('APPPATH', $application_folder.'/');
}
else
{
if ($application_folder == '')
{
$application_folder = 'application';
}
define('APPPATH', BASEPATH.$application_folder.'/');
}
/*
|---------------------------------------------------------------
| LOAD THE FRONT CONTROLLER
|---------------------------------------------------------------
|
| And away we go...
|
*/
require_once BASEPATH.'codeigniter/CodeIgniter'.EXT;
/* End of file index.php */
/* Location: ./index.php */
Server error Fatal error: Class 'Memcache' not found in /hermes/bosoraweb075/b2180/ipg.djsaifnycom/paly/system/application/controllers/home.php on line 21
function index()
{
$data['memcache'] = new Memcache;
$data['memcache']->pconnect('localhost', 11211) or $memcache = false;
//$this->output->cache(60);
$this->load->model('dx_auth/UserModel', 'user');
$this->load->model('homemodel');
$data['title'] = "Home";
if($this->dx_auth->is_logged_in())
{
$query = $this->user->get_user_by_id($this->session->userdata('DX_user_id'));
//now we get personalized
$row = $query->row();
$data['feedFilter'] = $row->feed_filter;
$data['feedView'] = $row->feed_view;
if($data['feedFilter'] == 'tagged')
{
$data['feedList'] = null;
$data['feedList'] = $data['memcache']->get('feed' . $data['feedFilter'] . $row->feed_view);
if($data['feedList'] == null)
{
$data['albums'] = $this->homemodel->getTagged();
}
} else {
$data['feedList'] = null;
$data['feedList'] = $data['memcache']->get('feed' . $data['feedFilter'] . $row->feed_view);
if($data['feedList'] == null)
{
$data['albums'] = $this->homemodel->getFeedNoUser();
}
}
//$this->output->enable_profiler();
$this->template->write('title', 'Home');
$this->template->write_view('content', 'home/index', $data, TRUE);
$this->template->render();
} else {
$data['feedFilter'] = 'all';
$this->load->helper('cookie');
if(get_cookie('unravel_feedView'))
{
$data['feedView'] = 0;
} else {
$data['feedView'] = 1;
}
//this would be generic feed here
$data['feedList'] = null;
$data['feedList'] = $data['memcache']->get('feed' . $data['feedFilter'] . $data['feedView']);
if($data['feedList'] == null)
{
$data['albums'] = $this->homemodel->getFeedNoUser();
}
$this->template->write('title', 'Home');
$this->template->write_view('content', 'home/index', $data, TRUE);
$this->template->render();
}
//$this->load->view('home/index', $data);
//$this->output->enable_profiler(TRUE);
}
the line is
$data['memcache'] = new Memcache;
Maybe the .htaccess is not in the correct directory.. you may want to check it, should be inside your project directory. if so,
put this in your .htaccess file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
You might want to also check your httpd.conf file and see if apache is allowed to "honor" your .htaccess file. Search for the AllowOverride command and change it from AllowOverride None (the default) to AllowOverride All.
ALSO: Do not forget to restart the httpd server afterwards.

Categories