One web page with multiple URL's using PHP - php

I was wondering how you could make it so that "website.com" has an index.php file that simply opens "website.com/home" but without changing the URL. I originally had it so that it used a PHP include of "website.com/home/index.php" inside "website.com/index.php" but when i had to use includes inside of "website.com/home/index.php" it stopped working as it was running the includes from "website.com/index.php" instead of "website.com/home/index.php" is there any way to simply send the file "website.com/home/index.php" and have it run from "website.com/home/index.php" without changing the URL? I would want to avoid having to use the absolute location to simplify it and so i can copy and paste the code into other directories without having to edit it accordingly.

Use chdir() to navigate to the correct folder, then include("index.php");

Related

how to access the file with modified url in php?

I want to access a file with my modified url without going via .htaccess
Let assume my file is in
root/newfolder1/xyz.php
So the url below is working fine
www.abc.com/newfolder1/xyz.php
I want to access the file in newfolder1 but with modified url like below:
www.abc.com/xyz.php // This I want but it is not working.
Any ideas or suggestions would be welcome.
You can either use symlink
Or you can make a php in your main folder
xyz.php
And include the php inside the newfolder1/xyz.php
Using this method make sure to change working dir to newfolder1 before including.

Running PHP script from other directory

I have a PHP script of about 10 .php files under the same directory. i.e.
www.domain.com/script_directory/
I wanted to run that script from another director and tried the following:
www.domain.com/some_directory/index.php
where index.php contains
<?php
include "../script_directory/index.php";
?>
Of course the above doesn't work.
Is there any way to set the working path of the script in PHP, similar to "HTML base Tag"?
Some files of the script are "called" via ajax and the others with direct links.
You said:
I want user to always stay at www.domain.com/some_directory
To do it, you can put a HTTP redirect in the index.php at the root, something like this:
<?php
header("location:/some_directory");
If using Apache, you can do it using .htaccess like this:
Redirect / http://www.example.com/some_directory
But all depends on the rules/behaviour that you want. Using include in PHP only insert a script into it, allowing to use the code/functions/classes that are into it.

PHP directly accessing the required files

So I work on a website and to make things easier I made specific files for every task (like: for the top menu I made menu.php) and then require(); them in the main files. All is good but I tried accessing in the browser /include/menu.php and it shows up. I don't want people to access them whenever they want, I just want to require them and to be available only through the main file.
The easiest way to prevent other php files from being accessed, is to define a variable in the main script:
define('IN_APPLICATION', true);
In all of your other files, simply add:
if ( !defined('IN_APPLICATION') )
die('You cannot access this file directly.');
An alternative way is to use an .htaccess file. If your server is running apache, this is all you will need. Simply put this file in your /includes directory.

Issues while importing php files

I am developing a website on a local test environment. From time to time I need to import classes or functions into my php pages. At first I used relative paths to import files, but for some (unexplicable) reason the PHP couldn't find those files. So I have thought it would be necessary use the following:
<?php require_once($_SERVER['DOCUMENT_ROOT'] .'/mywebsite/inc/libs/functions.php'); ?>
The problem is when I have to upload all the pages to my remote webserver, the PHP interpreter won't find functions.php in that position because there is no mywebsite subfolder , on the other hand I can't get rid of mywebsite subfolder, because that would leave me with http://localhost/inc/libs/functions.php which leads nowhere.
So that basically means I will have to manually readjust the path to make everything work. My question is: is there a way for PHP to detect the exact folder of my website so that I don't have to change paths everytime I need to upload a php file to my webserver?
What is the reason behind impossibility of use relative path?
<?php require_once(dirname(__FILE__).'/inc/libs/functions.php'); ?>
Set the base folder with all the files you want as an include path in php.ini.
That way, when you use include () or require (), it will automatically look in the included path.
www.php.net/manual/en/ini.core.php#ini.include-path

Including files in php not being found

I am working on a php project and I am having problems with including files.
I have a php script which is located at myapp/reports/index.php. When a form is submitted it performs an ajax post to another phpscript located at myapp/reports/phpHander/submit.php.
Submit.php then has to include a php script which is used to send an email. This is done to ensure that same code can be used over and over again without it needing to be typed for each time it is need. This php script is located at ../../administrator/classes/send.php.
Up to this point is working fine however the send.php script includes another file to get app config settings which is located in administrator/appConfig.php. The problem is this appConfig.php isn't being found even if I put in the full web address of http://localhost/myapp/administrator/appConfig.php.
What am I doing wrong. I am using the php include function to do this and its working for everything else but it seems to have a problem then including another script from a different location.
Thanks for any help you can provide.
http://localhost/myapp/administrator/appConfig.php is only URL path.
You need absolute filepath like C:/wamp/www/myapp/administrator/appConfig.php (Windows) or /var/www/myapp/administrator/appConfig.php (Linux)
Anyways best way is make a file "dirs.php" in your root application directory with constant:
define('ROOT_DIR', dirname(__FILE__));
where dirname(__FILE__) will be absolute path to your app directory.
With this knowledge you can include files in this way:
myapp/reports/index.php:
require_once('../../dirs.php');
include(ROOT_DIR . '/administrator/appConfig.php');
myapp/reports/phpHander/submit.php.:
require_once('../../../dirs.php');
include(ROOT_DIR . '/administrator/appConfig.php');
When you include another PHP script, all the paths are relative to the calling script. So, it sounds as though your script is at myapp/reports/phpHander/submit.php and includes ../../administrator/classes/send.php, which then includes another script in that same directory. In this case, you need to use the path "../../administrator/appConfig.php". Alternatively, you could use absolute paths relative to the filesystem's root.

Categories