I want to use the require_once function inside a php file but whenever i do it creates a fatal error and it halts the script. i have a modules folder where their are 4 folders which are pages, scripts,db and uploads. Inside the pages folder i have a file(home.php) where i want to include the other one in, the other file(connection.php) is inside of the scripts folder. I wrote this in home.php:
<?php
require_once('../scripts/connection.php');
?>
<section class="p-5">
<div class="container-lg shadow">
<?php
if(ISSET($_SESSION['success'])){
?>
<div class="alert alert-success">
<?php echo $_SESSION['success']; header("refresh: 2");?>
</div>
<?php
unset($_SESSION['success']);
}
?>
<div class="row row-cols-1 row-cols-md-3 g-4">
<div class="col">
<?php foreach($postSnkr as $rows => $postSnkr) { ?>
<div class="card">
<?php echo("<img src='uploads/".$postSnkr['slika']."'>");?>
<div class="card-body">
<h5 class="card-title"><?php echo($postSnkr['model'])?></h5>
<h6 class="card-subtitle mb-2 text-muted"><?php echo($postSnkr['barva'])?></h6>
<ul class="list-group list-group-flush">
<li class="list-group-item">Releas date: <?php echo($postSnkr['datum_izdaje']);?>
</li>
<li class="list-group-item">Size: <?php echo($postSnkr['velikost']);?></li>
<li class="list-group-item">Price: <?php echo($postSnkr['cena']);?></li>
</ul>
<a href="checkout.php" class="btn btn-primary" value="">Buy</a>
</div>
</div>
<?php } ?>
</div>
</div>
</div>
</section>
in connection.php:
<?php
if(!is_file('db/sneaker_haven.sqlite3')){
file_put_contents('db/sneaker_haven.sqlite3', null);
}
$conn = new PDO('sqlite:db/sneaker_haven.sqlite3');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>
and in index.php:
<body>
<div class="container-fluid">
<div class="wave"></div>
<div class="wave"></div>
<div class="wave"></div>
<?php
require_once("modules/pages/header.php");
?>
<br>
<div class="container my-5">
<?php
if(isset($_GET['page'])){
switch ($_GET['page']) {
case "login":
require_once("modules/pages/login.php");
break;
case "reg":
require_once("modules/pages/register.php");
break;
case "create_post":
require_once("modules/pages/create_post.php");
break;
case "profile":
require_once("modules/pages/profile.php");
break;
case "home":
require_once("modules/pages/home.php");
break;
default:
# code...
break;
}
}
if(isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true){
require_once("modules/pages/home.php");
}elseif(isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == false){
require_once("modules/pages/login.php");
}
?>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
</body>
I tried to use the require_once function inside of home.php but it did not work it halted the script error message: Closed without sending a request; it was probably just an unused speculative preconnection
fetal error:
require_once(../../modules/scripts/connection.php): Failed to open stream: No such file or directory in /home/gogi/Desktop/sc/Online-sneaker--Marketplace/modules/pages/home.php on line 2
[Thu Feb 23 19:37:46 2023] PHP Fatal error: Uncaught Error: Failed opening required '../../modules/scripts/connection.php' (include_path='.:/usr/share/php') in /home/gogi/Desktop/sc/Online-sneaker--Marketplace/modules/pages/home.php:2
Stack trace:
#0 /home/gogi/Desktop/sc/Online-sneaker--Marketplace/index.php(48): require_once()
#1 {main}
thrown in /home/gogi/Desktop/sc/Online-sneaker--Marketplace/modules/pages/home.php on line 2
[Thu Feb 23 19:37:46 2023] 127.0.0.1:59784 [500]: GET /index.php - Uncaught Error: Failed opening required '../../modules/scripts/connection.php' (include_path='.:/usr/share/php') in /home/gogi/Desktop/sc/Online-sneaker--Marketplace/modules/pages/home.php:2
Stack trace:
#0 /home/gogi/Desktop/sc/Online-sneaker--Marketplace/index.php(48): require_once()
#1 {main}
thrown in /home/gogi/Desktop/sc/Online-sneaker--Marketplace/modules/pages/home.php on line 2