How to include file from multiple directory - php

I am working in PHP. I tried hard to find a solution. Maybe some expert definitely gives me a proper solution.
'core/init.php' load / call all classes and functions. In bottom, other pages like 'index.php' , 'login.php' include the core/init.php. I already do that one. No problem is when admin login and in 'dashboard.php' he can see 'due_payment.php'. But i cannot include the 'core/init.php' file because it check in the '/admin' section.
I know it's something messy. Hope someone gives me a better Idea.

Related

Got 404 when redirect every url to index

So, I've been trying to do the Wikipedia url thing! Like making the url readable. Tried every .htaccess code out thire and contacted the Hostinger customer services to help, and nothing works! I don't know why.
So, i move to use the PHP, and i got work great on test.php page that i use the test on, but when i move the code to index.php it doesn't.
So, the why i did it is having a page with php simple code: if url[2] has s include s.php, else include x.php. and it works great when i go to www.website.com/test.php/s it will open s.php
However, when after i move the code to index.php and then i go to website.com/s i got an error as this page is not in the srver, but if i go to website.com/index.php/s it works.
So what do think i could fix it with? Or if there is any other why to do it. Thanks in advance ☺

Xampp not supporting php Include

I know this has been discussed a lot throughout the Stackoverflow.
But still I can't make it work for me, I've been searching for hours, no joke.
I always make my index.php page like this:
<? include ("header.php");?>
<? if($_REQUEST['p']==''){ include('home.php') }
if($_REQUEST['p']=='atelier'){ include('atelier.php'); }
if($_REQUEST['p']=='quem_somos'){ include('quem_somos.php'); }
if($_REQUEST['p']=='que_criamos'){ include('que_criamos.php'); }
if($_REQUEST['p']=='projetos3d'){ include('projetos3d.php'); }
if($_REQUEST['p']=='noticias'){ include('noticias.php'); }
if($_REQUEST['p']=='localizacao'){ include('localizacao.php'); }
if($_REQUEST['p']=='contactos'){ include('contactos.php'); } ?>
<? include ("footer.php");?>
It is supposed to always loads the header and footer.
On page opening it is supposed to load the home.php because there's no request and the menu of the website has links that are requests to other pages.
This always worked for me, I used to work on a ftp server but now I'm using XAMPP for this and it seems like the includes are not working at all.
Is there any file I could go and change some permissions or even better change the way I include the files to be supported on XAMPP?
EDIT: For those asking about how REQUEST works, I have a sample here, it might be easier to understand if you see the menu working: link
EDIT 2: By not supporting I mean: it displays a blank page, it doesn't load neither the header/footer nor the pages themselves
EDIT 3: I've enabled short_open_tag and allow_url_include in php.ini, still not working.
EDIT 4: I went to try easyphp and I still got the same problem... I'd rather not move from where I am and keep the XAMPP to find a solution for this.

Proper way to require file that requires db connection

I don't use php very often and was wondering if someone could answer this question for me.
I have a folder structure like so:
-pages/rightCol.php
-pages/privacyPolicy.php
index.php
In my index file I have a connection to the database like this:
ob_start();
require($_SERVER['DOCUMENT_ROOT'] . "/inc/db.inc.php");
That works fine.
I wanted to separate out some repeated code between pages so I created the rightCol.php file. It needs the connection to the database. So right now I create a query result at the top of the index file and use the statement:
This works.
I also wanted to include it in the privacyPolicy.php page. This does not work because I do not want to put the query code at the top of every page that requires the rightCol.php file.
I would like to put the db stuff inside the rightCol.php. When I try this, then my privacyPolicy.php file works but then my index breaks. Probably because I require the db file twice, once at the top of the index and once in the rightCol.php file.
How can I set this up properly where I do not need to repeat code.
Thanks
EDIT
I changed my call to use require_once.
The privacyPolicy.php page works fine but when I view my index.php it has errors.
Error: No DB selected.
Include the db/inc.php only at the start of your index.php and open the connection. That way, it will stay open throughout the whole script. Then just close it at the very end of your site;
If you are having problem knowing where to include and not ( and still for some unknown reason want to include it more then once ), then get used to require_once method. This way the file will be included only once and the 2nd attempt will be ignored.
Well, the quick way to solve the problem is to use require_once. But i highly recommend that you use a micro-framework like Slim.

Get Page URL In Order To Use It To Include

So I made a script so that I can just use includes to get my header, pages, and then footer. And if a file doesnt exist a 404. That all works. Now my issue is how I'm supposed to get the end of the url being the page. For example,
I want to make it so that when someone goes to example.com/home/test, it will automatically just include test.php for example.
Moral of the story. How to some how get the page name. And then use it to "mask" the end of the page so that I don't need to have every URL being something.com/home/?p=home
Heres my code so far.
<?php
include($_SERVER['DOCUMENT_ROOT'].'/home/lib/php/_dc.php');
include($_SERVER['DOCUMENT_ROOT'].'/home/lib/php/_home_fns.php');
$script = $_SERVER['SCRIPT_NAME']; //This returns /home/index.php for example =/
error_reporting(E_ALL);
include($_SERVER['DOCUMENT_ROOT'].'/home/default/header.php');
if($_GET["p"] == 'home' || !isset($_GET["p"])) {
include($_SERVER['DOCUMENT_ROOT'].'/home/pages/home.php');
} else if(file_exists($_SERVER['DOCUMENT_ROOT'].'/home/pages/'.$_GET["p"].'.php')) {
include($_SERVER['DOCUMENT_ROOT'].'/home/pages/'.$_GET["p"].'.php');
} else {
include($_SERVER['DOCUMENT_ROOT'].'/home/default/404.php');
}
include($_SERVER['DOCUMENT_ROOT'].'/home/default/footer.php');
?>
PHP by itself wouldn't be the best choice here unless you want your website littered with empty "redirect" PHP files. I would recommend looking into the Apache server's mod_rewrite module. Here are a couple of guides to get you started. Hope this helps!
The simplest way would be to have an index.php file inside the /home/whatever folder. Then use something like $_SERVER['PHP_SELF'] and extract the name if you want to automate it, or since you are already writing the file yourself, hardcode it into it.
That however looks plain wrong, you should probably look into mod-rewrite if you are up to creating a more complex/serious app.
I would also recommend cakePHP framework that has the whole path-to-controller thing worked out.

Need help with Code Igniter [did not found page created]

so I hope someone would help me.
My first page is leave_app.php. In this page I put a link to apply leave, using the normal code:
New Leave Application
However the link didn't display as I had wished for.
"The requested URL /ci/add.php was not found on this server." and the link in the browser leads to this "http://localhost/ci/add.php".
I don't know why the server didn't find the page. I already made add.php page, also add at the leave_app controller the function add().
In the config.php file I put $config['index_page'] = '';
I have asked around but no one can help. I have already surfed around, but still don't know how to solve.
The beauty of Codeigniter is to rewrite urls in a clean manner, so all things go through index.php and the corresponding controller is loaded
Try accessing it like so:
/ci/index.php/add
Make sure your controller is in application\controllers\add.php and named Class Add

Categories