Shivamyadav's avatar

Why i am getting this error? In PHP

This is the my index.php file code

<?php
echo $_SERVER['REQUEST_URI'];
$uri = parse_url($_SERVER['REQUEST_URI'])['path'];
$routes = [
    '/' => '/index.php',
    '/register' => '/register.php',
    '/login' => '/forms/login/login.php',
    '/logout' => '/forms/logout/logout.php',
    '/admin/profile' => '/forms/admin/index.php',
    '/contact' => 'controllers/contact.php',
];

// function routeToController($uri, $routes) {
if (array_key_exists($uri, $routes)) {
    require __DIR__ . $routes[$uri];
} else {
    echo "404 page not found!";
}
?>

as i have written in line 2 echo $_SERVER['REQUEST_URI']; when i run this in the browser i am getting this error and why this line is executing many times ? 🤷‍♂️

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
it shows this line means i have set this to main index page call on this url. It shows many time untill the memory exhausted why ?
Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 12288 bytes) in C:\laragon\www\php e-commerce project\index.php on line 19
0 likes
12 replies
Shivamyadav's avatar

@jlrdw but i my case, I am unable to understand the logic behind why it's running to much time with the based on given url .. used this and problem solved

require_once __DIR__ . '/router.php';

but going with when require keyword

require __DIR__ . '/router.php';

then it shows the error as i mentioned in this forum starting to it.. I want to know Why it behaves like this sir? By the way thanks for commenting 🙏

jlrdw's avatar

@Shivamyadav that shouldn't make a difference. I suggest you make sure you have everything setup correct. There is a difference in php artisan serve and apache or nginx.

2 likes
tisuchi's avatar

@shivamyadav I think it's because of '/' => '/index.php', in your array. It should create an infinity loop.

Can you try removing this item from the array? It should be like this:

$routes = [
    '/register' => '/register.php',
    '/login' => '/forms/login/login.php',
    '/logout' => '/forms/logout/logout.php',
    '/admin/profile' => '/forms/admin/index.php',
    '/contact' => 'controllers/contact.php',
];
2 likes
Shivamyadav's avatar

@tisuchi yes it worked, but what about that sir when it doesn't find the home url from the $routes array it will always render me on a 404 page, any idea🤷‍♂️

Shivamyadav's avatar

@Snapey yeah sure sir, but when I use this means add this in my $routes array then I get the error I have mentioned at the top of this discussion forum have a look sir please🙏.

Shivamyadav's avatar

@Snapey or here it is sir have a look

This is the my index.php file code

<?php
echo $_SERVER['REQUEST_URI'];
$uri = parse_url($_SERVER['REQUEST_URI'])['path'];
$routes = [
    '/' => '/index.php',
    '/register' => '/register.php',
    '/login' => '/forms/login/login.php',
    '/logout' => '/forms/logout/logout.php',
    '/admin/profile' => '/forms/admin/index.php',
    '/contact' => 'controllers/contact.php',
];

// function routeToController($uri, $routes) {
if (array_key_exists($uri, $routes)) {
    require __DIR__ . $routes[$uri];
} else {
    echo "404 page not found!";
}
?>

as i have written in line 2 echo $_SERVER['REQUEST_URI']; when i run this in the browser i am getting this error and why this line is executing many times ? 🤷‍♂️

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
it shows this line means i have set this to main index page call on this url. It shows many time untill the memory exhausted why ?
Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 12288 bytes) in C:\laragon\www\php e-commerce project\index.php on line 19
Snapey's avatar

@Shivamyadav you have already had that answered, you have an infinite loop

Do you have an .htaccess file

1 like
Shivamyadav's avatar

@Snapey but how it goes to an infinity loop sir🤷‍♂️ unable to understand, yeah but .htaccess in inside the php directory not in my project folder

KristianJust's avatar

If you run out of memory and have no clue how it happens, you can add a simple logging entry like this

file_put_contents(__DIR__ . '/debug.log', 'Memory at line ' .__LINE__ . ' ' . memory_get_usage(), FILE_APPEND);

Add that line at different locations in your script.

When the memory is exhausted, read your debug.log file to get a picture of the pattern.

2 likes
jlrdw's avatar

Why isn't the routes in a routes file? Hard to tell what you are trying to do.

2 likes

Please or to participate in this conversation.