Auto change of pad in a fixed header - php

Okay, first; I'm relatively new to php.
I know the basics but nothing too fancy.
I'm developing a website where the header and the footer stay the same on which ever page you are.
But the problem is this (obviously).
The index.php looks like this:
<?php include("header.php") ?>
// Body content
<?php include("footer.php") ?>
In the header.php there's also the < head> tag.
When I change a page that is in an underlying folder, the css path ofcourse stays the same (so without the "../" ).
And there's my problem, is this fixable or do I have to exclude the < head> tag from the header.php?
Thank's in advance.

The easiest way to solve this is to use absolute paths like this:
/path/to/css/file.css

If the header page stays always in the same directory, I think that there is no problem with that.
In the pages that are in the other folders you just have to point to where the header is and then the header will be in the right directory to call the css's.
Like:
<?php
include("header.php")
?>
And in a subfolder:
<?php
include("./header.php")
?>
Have you tried this?

I suggest to do a different approach.
Any request is handled by one file, so you index.php
in there you have
<?php include("header.php") ?>
// Body content
<?php include("footer.php") ?>
you can then create your directory structure and catch the script via an URL $_GET parameter
e.g. index.php?request=page1
you can then handle this in PHP saying
switch ($_GET['request']){
case 'page1' : require './sub/page1.php';break;
....
default: require 'real_index.php';break;
}
Then you can make your SEO-urls by using the .htaccess file. This is basically a rerouting of the url (mod_rewrite in Apache)
There you should have something like this:
RewriteEngine On
RewriteRule ^(.+)$ index.php?request=$1 [QSA,L]
So any request is then redirected, to the script you want, but on the server side you always stay on the same directory level like index.php is.
e.g http://localhost/page1 is then redirected to http://localhost/index.php?request=page1
For a bit more explanation maybe this video helps BasicMVC tutorial
Its a bit fast for beginners, but very good explained.

Related

Managing Links within PHP Includes using htaccess

Thanks for reading!
I am managing a header with links using a PHP include. It is within a folder /includes/header.php.
Here's an example of what header.php looks like:
<nav>
<ul>
<li>Home</li>
<li>Page</li>
</ul>
</nav>
When I add the include to a file within the root directory, like /index.php, I add it like so: <?php include_once("header.php"); ?>. This all works fine, and the links point where they need to.
When I do the same thing but with a file in a subdirectory, for instance a file called /foo/page.php I will add the include like this: <?php include_once("../includes/header.php"); ?> - this way it grabs the file correctly.
My problem is that all of the links in the header.php file aren't going where I want them to. I found some information about using a set environment function in .htaccess, but I don't know what to make of it.
If you have an answer to this problem I'd love to hear it! Thanks!
Start all the links in the header from the root web directory.
Just do;
"/index.html"
"/subdirectory/link.html"
So basically just start all the links with a forward slash, as without it, it will look for the page within its current directory.
You can set the base url in your HTML head.
Store the base url of your application in a config file or database and then use it to build absolute links not relative ones. For example you have a file like config.php:
<?php
$baseUrl = "http://yourdomain/yourapp/";
And in header.php:
<?php include_once("config.php"); ?>
Page
It may seem inconvenient having to edit a file in case you move your application, but this way your links will work in any directory any time, and as your application grows there will be some other things like DB access that also have to be changed if you move your application, and can be stored in the same config file.

Is it possible to use .htaccess to make custom Index Of pages? If so how?

You know how when you go to a url on a server and the directory doesn't have an index.* file or a default.* file it shows you a list of the directorie's contents? I was wondering if there is any way to customize the way that index looks or theme it to fit your site. For instance I'd want to add the php
<?
include 'template.php';
head();
?>
Before the listing. And
<?php
foot();
?>
After. Can this be done?
I have never done this before, but this shows how to do it: http://www.webmasterworld.com/apache/3589651.htm via .htaccess.
Edit: Maybe this (snapshot) shows exactly what you want, it's a walk-through of how to embed header and footer in the listing page.
Sure, you can add your own DirectoryIndex file which can even be PHP.
In you .htaccess file:
DirectoryIndex my-dir-listing.php
Do inside the my-dir-listing.php file whatever you need to do the directory listing.
If you want to modify the header of the existing listing, take a look into the HeaderName directive.

how can i make script like this: <script src="mysite.com/dir/"></script>

i know, this looks stupid, but i am a new one in scripting so please help, i want my src not be ending with file with extension. like:
<script src="mysite.com/dir/"></script>
and NOT like this:
<script src="mysite.com/dir/script.js"></script>
is this done with HTACCESS or with what?
P.S. I want to create something like a hitcounter and dont want athers to see my script code
here is an sample:
<script type="text/javascript" src="http://topsite.ucoz.com/stats"></script>
if you run this in html it will sent a mini banner and an hiden iframe with that page.
Use url rewrites in your .htaccess like this:
RewriteEngine On
RewriteRule ^dir/$ /dir/script.js [NC]
The problem with this is that /dir/ cannot be used for anything else.
What you could do is something like this:
RewriteEngine On
RewriteRule ^script/$ /dir/script.js [NC]
However this does not solve the problem of people seeing your code at all.
The only solution to that is either code obfuscation or using php (or some other server-side language)
You can configure your web server to have a default page when you call a directory.
This is usually done for index.html, index.php, ...
And here say this default is script.js the server should then deliver it by default
The first one is to a directory and not a file. It doesn't really make sense unless your thinking how on most servers http://abc/index.html goes to http://abc/
Cant really do it and cant see why you'd want to.
Either use the htaccess code listed to rewrite your url or simply fill your index.php with javascript.
and actually, using a url-rewrite would make it pointless having an index.php/index.html there in the first place.
mysite.com/page.php:
<script type="text/javascript" src="/dir/"></script>
mysite.com/dir/index.php:
alert('hello world');
also the sample you listed shows an example of someone using a file, not a whole directory. they simply leave off the extension to their file. Extensions only have semantic value.
I think the main goal here is 'to hide javascript from the user' right?
If you want to hide a javascript, you may want to lookup for 'javascript obfuscation'.
You can't really hide javascript, because it must be able to run on the client browser.
Even if you set the default url, as answers stated above, the javascript file will be downloaded.
However, you can obfuscate javascript files so that it will be hard for humans to read while machines can execute it.
There is a question in SO that ask about it.
How can I obfuscate (protect) JavaScript?

.htaccess redirect subdirectory as query string in php

This is hard to explain, so hopefully I'm understood in my question.
(1) I want to create "SEO friendly" links that remove the query string from a web site. There is only one variable, let's call it "page". Here is the following code for my .htaccess file.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ index.php?page=$1
This works in providing the proper redirect. So /applications/ will send to index.php?page=applications.
(2) My index.php will include a view page based on the value of $_GET['page']. Here is some sample code below:
switch ($_REQUEST['page']) {
default:
include ("home.php");
break;
case "apps":
include ("apps.php");
break;
}
There seems to be no problems so far.
(3) Let's make apps.php an exact copy of home.php. home.php loads just fine, but apps.php will not load linked CSS and JScript pages. When apps.php is loaded, it thinks it is in the /apps/ directory. To load the linked pages, I would need to insert a "../" in front of the file name. Then it displays correctly.
So my question is -- How can I properly write the .htaccess file so the home.php and apps.php page can be identical files and produce identical results, instead of the apps.php file being treated as if it were in the /apps/ directory?
First, I should apologize as I don't have a solution which involves making changes in the htaccess. My solutions are of a different nature.
I think the problem can be solved if you have a config variable,preferably in a config file, which will hold the root folder for images, js etc. Most of the time its public_html, the document root, where the url of your website points to. so your config variable could look like:
$base_url = 'http://www.mywebsite.com/';
The config file should be included in index.php unconditionally.
So, when you include any js or images, you do it like this:
<script type="text/javascript" src="<?php echo $base_url;?>js/global.js" />
<img src="<?php echo $base_url;?>images/gradient_green.jpg" />
If you include the config file in index.php, all the files you include based on switch-case conditions, will be able to use the $base_url variable.
Another possible solution is to use the base tag. Look it up here:
http://www.w3schools.com/TAGS/tag_base.asp
I hope this helps.
use absolute urls for js, css and images on your pages (starting with a slash).
/js/main.js instead of js/main.js
You can't do that with .htaccess unless you do an external redirect (by adding the [R] flag to your RewriteRule). But then you expose the query string, which is what you wanted to avoid in the first place.
The reason it can't be done: It is not apps.php which "thinks it is in the /apps/ directory" - it's the browser which "thinks" that. In the page source generated by apps.php, you send relative URLs back to the browser, and now the browser will request these resources relative to the location of the page it asked for. For the browser, the page it got is in /apps/, no matter what rewriting you applied internally on the server side.
So the options you have are:
Do an external redirect with your .htaccess (and defeat your original purpose ;-)
Change the URLs dynamically with PHP while processing apps.php etc, as you said (prefixing ../ to the URLs)
Use absolute URLs, just as #nobody has suggested in his answer.
The last one is the only real option IMHO.

How could you create a "content_for" equivalent in PHP?

I've been working on a small page in PHP, one that doesn't need the power of a full-fledged framework behind it. One thing that I'm really missing from previous work in Ruby-on-Rails is the ability to effectively pass content up the page using "content_for".
What I was wondering is, how could you create a page lifecycle that would accomplish this same effect in PHP?
So, here's a simple example:
Let's say you have a template that defines an index page, just a recurring header and menu you want to use on all your pages. So your index.php file looks basically like this:
...header stuff...
<body>
<?php include $file.'.php'; ?>
</body>
...footer stuff...
EDIT: Thanks for the tips on URL security, but let's just assume I'm getting the user request safely :)
Now, lets say in the header you want to put this:
<head>
<title><?php echo $page_title; ?></title>
</head>
It would be nice to be able to specify the title in the included file, so at the url http://example.com/index.php?p=test you're loading test.php, and that file looks like this:
<?php $page_title = 'Test Page'; ?>
... rest of content ...
Now, obviously this doesn't work, because the including page (index.php) is loaded before the variable is set.
In Rails this is where you could pass stuff 'up the page' using the content_for function.
My question is this: What would be the simplest, leanest way that you all can think of to effect this kind of 'content_for' functionality in PHP?
Ideally I'd like suggestions that don't involve strapping on some big framework, but some relatively light boilerplate code that could be used in a lot of different applications.
Never do include $_GET['p']. This opens a huge security hole in your site, as include accepts filenames and URLs, so anybody would be able to read any file on your site and also execute any code on your server. You may want to check and sanitize the value first.
If you need something simple, you may put header and footer in separate files, execute your test.php which would set the variables, capture its output using output buffering, then include the header, output the middle part and include the footer. Example:
<?php ob_start(); ?>
<body>
<?php include $filename.'.php'; ?>
</body>
<?php $content = ob_get_clean();
include 'header.php';
echo $content;
include 'footer.php';
?>
If I understand you correctly (I have not used RoR extensively), you could put your data in a variable or a function. If your content was in a variable, your "test.php" could simply hold all your variables and you could load it at the very beginning of your index file (likewise for a function depending on how complicated your needs are; if you're doing a lot of extra work, you may need to use a function as a variable won't work).
For example, your test.php would look something like this:
<?php
$page_title = "Test Page";
$page_content = "Some sort of content";
// Or
function page_content()
{
// Run some functions and print content at the end
}
?>
Then, in your index.php
<?php include $_GET['p'].'.php'; ?>
...header stuff...
<title><?php print $page_title; ?></title>
<body>
<?php print $page_content; ?>
<!-- OR if function -->
<?php page_content(); ?>
</body>
...footer stuff...
This way everything should load properly. You could also split things up, but that would complicate your structure (especially if there is no need for an elaborate framework, this would be unnecessary).
Good luck!
Dennis M.
Are you worried about XSS? Or are you going to filter/whitelist the "filenames" from the query string?
My answer would be to use mod_rewrite -- if you're using PHP, you're likely using Apache!
You could filter out files with a RewriteCond and your RewriteRule could be:
RewriteRule /index.php?p=(.*)$ $1 [L,QSA]
This may be a different approach than the PHP functionality you were looking for, but it comes to mind...

Categories