How to create a anchor link to different .php file? - php

I'm new to php and hope you can make me figure out whether I'm trying to do anything impossible.
I have two folders 'public' (root directory), and 'library' (all php files here), these folders are in same level of folder hierachy. my public/index.php is basically loading another php file (say aa.php) which is in 'library' folder on the loadup. Now I need to create a anchor link to file call bb.php which is also inside library folder.
I tried create anchor as follows
echo " my bb file
But I'm getting 404 error saying localhost/bb.php can not be accessed. I guess this is because bb.php file is not with in root directory and server is preventing direct access to this file.
Please help me to overcome this problem.
Thank you

If library/ and public/ are at the same level, the webserver will not be able to serve the files in library/. Typically, files in a library directory would be included by files in the public web folder.
If you need to use the bb.php directly, you will have to move it to public or a folder within public. And then from within public/bb.php you can include library files
/* public/bb.php */
include("/path/to/webroot/library/file.php");

Move bb.php into the folder "public".

Related

How can i deny access to request url in htaccess?

I have problem I'm using my folder as 'includes' and there are all files with extension .php so I wanna block this folder, when someone "hacker" want to write in url path to for instance file responsible for connection to database. But other files can using this files to make a diffrent operations.
inside your "includes" directory create a file ".htaccess" with a row "deny from all". It will prevent browser access to your files inside Includes directory.

cannot directly call the php file inside folders using the directory path

I have one root folder called GASS where I put all my php files and other related folders (templates,images,js,fonts,css)inside. When i try to run my project in localhost, http://localhost/GASS/alarm_A16GSM.php everything went smoothly. I wanted to change the URL to be more specific, http://localhost/GASS/alarmsystem/16zone/A16/overview.php thus i rename the php file and put it inside folders.
GASS
alarm-system
16-zone
A16
overview
However when i try to run the new URL,the page shows error.This is the error message:
Warning: include(templates/header.php): failed to open stream: No such file or directory in .....
Code for the first URL where the page load successfully.
<div class="overview"><a href="alarm_A16GSM.php" id="overview-selected"><span>
Code for the new URL where the page shows error.
<a href="alarm-system/16-zone/A16/overview.php" id="overview-selected">
It seems like i need to configure something which i do not know what it is.
How am i going to load the page successfully using the new URL? How am i going to traverse four levels up to the root directory so that the page load successfully? Why i cannot directly call the php file using the(alarm-system/16-zone/A16/overview.php) path?
p/s: sorry for my bad English.
It looks like there is a line in your Php file, probably like
include 'templates/header.php';
Include can't find the file using that relative path, because you moved the calling file.
Probably you could change that to
include '../../../../templates/header.php';
To get back down to the GASS folder that apparently has a folder called 'templates' with a file 'header.php' that is required.
An absolute path would be good, instead but it refers to the filesystem path, not webserver path - so you'd need to know your web root folder name on the server.
Copying all the folders (templates,images,js,fonts,css) to the folder overview will solve the issue. Now there is no template file on the folder 'overview' so header.php is failed to load. Another option is create a file save all the included file path and call this file.

Linking file (stylesheets, script files, images and etc..)

I have a problem on linking files such as stylesheets, images, database connection file(db.php), script files and etc.. because they are located outside the file where they are included/linked.
For example assuming this is the location of the main file where everything will be called:
my website directory/admin/thefilewhereeverythingwillbecalled.php
in thefilewhereeverythingwillbecalled.php, I must call the db.php which is located outside the folder that contains thefilewhereeverythingwillbecalled.php
e.g. my website directory/-this is the directory where the db.php is located-/thefilewhereeverythingwillbecalled.php - ditto the style sheets and script.
However the stylesheets and script are in the folder cssandjs (contains all stylesheets and script files) which this folder are located before the location of the thefilewhereeverythingwillbecalled.php
e.g. my website directory/-here is where the cssandjs folder is located-/thefilewhereeverythingwillbecalled.php
Generally I'm just having a problem on linking files which is outside from the file where it called those files. Can someone give me an idea how to link it please?
I tried this:
../cssandjs/style.cssand ./cssandjs/jquery.js but none of them work
If I'm correct, it's 2 directories up. Try this :
../../cssandjs/style.css
This will work if /cssandjs/ is in your website directory.
You should be a bit more specific with your directory names instead of -here is where this is located-
Can't seem to figure out which are in the same folder.
try to include full path to the files.
Try referencing the files like including the directories and files with
$_SERVER['DOCUMENT_ROOT']."/dirName/file.ext";
or use relative paths "../dirnName/file.ext";
first method is preferred

Including a javascript file outside of doc root using zend framework

I'm trying to include a javascript file in a phmtl view script file using the zend framework. Both the javascript file and the phtml file are part of a php library and located outside the doc root folder of my project. So the file structure looks like
/var/www/vhosts/project/
/var/www/vhosts/libraries/my-lib/view/viewscript.phtml
/var/www/vhosts/libraries/my-lib/js/javascript.js
/var/www/vhosts/libraries/my-lib has been added to the PHP paths using set_include_path. In viewscript.phtml, I use the following line to include javascript.js.
<?php $this->headScript()->appendFile('js/javascript.js'); ?>
For some reason, javascript.js is not included, even if I specify the absolute path instead of a relative path. Instead, I get a whole copy of my webpage inside a tag in the head section. If I put javascript.js into the doc root folder /var/www/vhosts/project and change the appendFile() path, it works just fine. How can I include javascript outside of doc root?
Based on previous questions you've been asking I think your directories are something problematic for you.
here is a functionnal and secure example or directory organization for Zend Framework (partial)
var/
www/
vhosts/
otherproject/
project/
src/ <-- maybe some project src can be here and not in your libraries
htdocs/ <--- real apache document root
css/
js/
var/
log/
sessions/
etc/
doc/
libraries/
Zend/
my-lib/
js/
So apache documentRoot is /var/www/project/htdocs. Here we can find the index.php file, the only php file available on public access.
In htdocs/js & htdocs/css you can put some of your project js & css files. And then you've got the problem of css and js files of your external php libs that are now completly outside of the web root.
What I usually do is, like others have said here, links from external directories to the js directory inside the web root. But to be more precise and keep things well organized here what you should do there:
ln -s /var/www/project/libraries/my-lib/js /var/www/project/htdocs/js/my-lib
ln -s /var/www/project/libraries/my-lib/css /var/www/project/htdocs/css/my-lib
And you should do it for all external lib having files that should be in the document root.
This way the base url for the js files of my-lib is /js/my-lib/.
Do not fear of using symlinks (junctions on windows), you can even store them in subversion repository. Just check that your apache configuration allow symlinks (Options +FollowSymlinks)
The path provided in appendFile() is relative to the site's document root (eg, your 'public' folder). It will not pick up on the php include_path.
You could move the js file into the doc root, create a symbolic link to it in the doc root, or you could read the file using php and output it's contents as a <script> tag.
form your path , i can tell your are using linux
so you can use symlink like this :
ln -s /var/www/vhosts/libraries/my-lib/ /var/www/vhosts/project/mylib/
therefor you can append the files :
<?php $this->headScript()->appendFile('/mylib/js/javascript.js'); ?>
and tada , its done
The tag that will be added to the page is a reference for the browser where to look for the JavaScript file.
JavaScript is a client side language, it runs on the users computer and is interpreted there, so the user needs to be able to access the file, hence it needs to be inside the root path as the user (client) should not have access to your application dir.
You could save a PHP file in your doc root and use that to get your JS:
getJS.php (saved in the doc root):
<?php
header("Content-type: text/javascript");
include_once '/../var/www/vhosts/libraries/my-lib/js/someJSfile.js';
?>
Then in your code:
<?php
$this->headScript()->appendFile('getJS.php');
?>
You could include switches to include different JS files or whatever you wanted, I haven't tested this for functionality, but the file when clicked does get the contents of the JS file.
Note: If this is for security reasons, it won't take much to get the contents of the file the user wants!

PHP cannot find my php files if not under htdocs folder

By someone's advice I've put all my PHP files in a separate folder (inc) on the same level as htdocs. Only index.php is left in htdocs. So, it's like this:
C:\myproject\htdocs
- index.php
C:\myproject\inc
- login.php
- util.php
- register.php
...
Now, when I go to localhost in my browser index.php is processed and shown correctly. But any links to other php files are not found. I tried to prepend links with "inc", but they're still not found. What should I do?
My php.ini file has this line (it's on Windows):
include_path = ".;C:\myproject\inc"
The point of an include directory is to put files you don't want to be accessible by a Webserver. If login.php needs to be accessible via a URL like:
http://yourdomain.com/login.php
then don't put login.php in the include directory. Putting util.php in an include directory makes sense because you never want this:
http://yourdomain.com/util.php
You can't put web-accessible files outside the htdocs folder, you should be using the 'inc' folder for files like 'database_functions.inc' which should not be opened directly in your browser:
http://localhost/index.php // directly accessible - goes in htdocs
http://localhost/login.php // directly accessible - goes in htdocs
http://localhost/register.php // directly accessible - goes in htdocs
http://localhost/util.php // you don't want people loading this file directly - put in 'inc'
http://localhost/database_functions.php // you don't want people loading this file directly - put in 'inc'
I believe you need to escape the backslashes in your php.ini -- so it should be C:\\myproject\\inc. But as others have pointed out, you won't be able to use a browser to access the PHP files in your include directory, because the web server will not allow access to a directory outside the htdocs tree.

Categories