http_response_code(400) in file_exists bug when using variable (in if block)
<?php
include 'system/func/sessions.php';
include 'system/func/template.php';
include 'system/func/db.php';
include 'system/func/core.php';
include 'system/func/get-func.php';
include 'system/func/post-func.php';
include 'system/func/all-func.php';
function router(){
$path = $_GET;
$path = html_clear($path);
$allPath = [
'index' => 'index',
'prod' => 'prod-detail',
'cat' => 'cat',
'brand' => 'brand',
'contact' => 'contact',
'faq' => 'faq',
'blog' => 'blog-detail',
'blogs' => 'blogs',
'check' => 'check',
'pay' => 'pay',
'success' => 'success',
'warn' => 'warn-file',
'account' => 'account-detail',
'fav' => 'fav',
'404' => '404'
];
$parsePath = explode('/', $path['u']);
if(!isset($parsePath[0])){
$parsePath = '404';
}else{
if(isset($parsePath[1])){
$_GET['q'] = permalink($parsePath[1]);
}else{
$_GET['q'] = '';
}
if(isset($allPath[$parsePath[0]])){
$_GET['u'] = permalink($allPath[$parsePath[0]]);
}else{
$_GET['u'] = '404';
}
}
}
if(empty($_GET['u'])){
$_GET['u'] = CURRENT_HOME;
}
if(!isset($_GET['u'])){
$_GET['u'] = '404';
}
router();
/**
* NOT WORKING -->
*/
if(!file_exists('view/templates/'.CURRENT_DIR.'/' . $_GET['u'] . '.php')){
http_response_code(404);
include 'view/templates/'.CURRENT_DIR.'/404.php';
}
/**
* WORKING -->
*/
if(!file_exists('view/templates/default/file_name.php')){
http_response_code(404);
include 'view/templates/default/404.php';
}
include 'view/templates/'.CURRENT_DIR.'/' . $_GET['u'] . '.php';
?>
My Htaccess file:
Options +FollowSymlinks
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^(.+)$ index.php?u=$1 [QSA,L]
</IfModule>
`
PHP Ver: 8.1.1
OS: Windows 10 Pro
Could it be because$_GET['u'] is not defined?
Could it be because
$_GET['u']is not defined?
I didn't feel the need to share. I'm correcting. My fault.
@ReturnMe Could you var_dump($_GET['u']); just to be completely sure it is what you expect it to be? What does the URL look like? This seems like an unlikely bug.
@ReturnMe Could you
var_dump($_GET['u']);just to be completely sure it is what you expect it to be? What does the URL look like? This seems like an unlikely bug.
I wrote my full codes, I'm adding htaccess below. I'll add the var_dump results shortly.
Please also note your code is probably vulnerable to inclusion of unintended scripts by adding ../ to the u GET parameter.
Please also note your code is probably vulnerable to inclusion of unintended scripts by adding
../to theuGET parameter.
Output of var_dump just before file_exist:
array(2) {
["u"]=>
string(5) "asasfaasasa"
["q"]=>
string(0) ""
}
I think I cleared well enough.
Including the 404 page (So, the correct if block is working.). But it doesn't make the HTTP response code 404. But, by removing the variables; If I write by hand. It's making the HTTP code 404.
I think I'll take a video.
@ReturnMe Your previous edit said u was index, which would explain why file_exists(... $_GET['u'] . '.php') would return true. This would indicate that most likely something with your Apache configuration isn't ok and your $_GET params are getting lost. You can check by var_dump($_GET); at the top of your script and opening an URL with ?foo=bar.
@ReturnMe Your previous edit said
uwasindex, which would explain whyfile_exists(... $_GET['u'] . '.php')would returntrue. This would indicate that most likely something with your Apache configuration isn't ok and your$_GETparams are getting lost. You can check byvar_dump($_GET);at the top of your script and opening an URL with?foo=bar.
Ahh, absolutely! I already have my existing file, 404.php YES!
I'm so sorry, I took your time. You are perfect. I am very grateful, thank you very much.
I realized after posting the video :( I understood much better with your comment. Again, I promise I won't report bugs while I'm sleepless :(
@ReturnMe Don't worry, it's fine. Happy to have helped.
@ReturnMe Don't worry, it's fine. Happy to have helped.
You are perfect.