Say i have a .html file that contains a web design and has variables in it like $time $comment $title.
how would i enter them variables from a php file
Sorry i cannot really explain it well enough but i hope somebody understands
Well.. I'll just guess what you want. Since the question isn't very clear.
In a PHP script, load the HTML in with something like file_get_contents, then use str_replace to replace the 'variables' in the HTML, then echo the new HTML.
Or, don't use an HTML file, use a PHP file, and just use echo where you want the variables to be.
Or... use something like smarty templates which is probably more advanced than you're looking for.
Learn PHP
Learn an MVC Framework of your choice
Change the .html file into .php
Put it in with your "Views"
Call it from your Controller (defining your view variables)
You can get apache to parse .html files as PHP by adding this directive to your .htaccess (or httpd.conf):
AddType application/x-httpd-php .htm .html
It might be necessary to change this line in your httpd.conf:
AllowOverride None
to:
AllowOverride All
That will enable .htaccess overrides to the apache configuration.
This answer assumes two things:
1 - That I understood the question.
2 - That you are in fact using Apache web server.
I also take a chance that I understand you right. If you think of a template system for PHP, I recommend you Smarty, which makes it very easy to divide HTML from php
Why would I use Smarty (when PHP is a template engine)?
Some might argue that Smarty does what
PHP can do already: separate the
presentation from business logic. The
PHP programming language is great for
code development but when mixed with
HTML, the syntax of PHP statements can
be a mess to manage. Smarty makes up
for this by insulating PHP from the
presentation with a much simpler
tag-based syntax. The tags reveal
application content, enforcing a clean
separation from PHP (application)
code. No PHP knowledge is required to
manage Smarty templates.
This sounds more like a utilization problem. As others have noted, static html files are just returned as such, static files. If you want the variables replaced whenever those files are accessed, then define a handler like that from your .htaccess:
RewriteRule (.+\.html) vars.php?file=$1
And a template function (vars.php) to works on those placeholders:
<?php
// define all placeholder variables here
$html = file_get_contents(basename($_GET["file"]));
print preg_replace('/\$(\w+)/e', '$GLOBALS["$1"]', $html);
?>
But it's certainly easier to just rename your .html files into .php, and put a heredoc around them:
<?php
echo<<<END
<html>$vars</html> ...
END;
?>
Related
I have the following structure for my site:
./u/
./u/profile.php
./u/comics.php
profile.php is filtered with the viewer's username. For example, the account Venk would be ./u/Venk. I need comics.php to somehow be a subfolder of this but I'm not sure how to go about doing so. It should end up looking like this: ./u/Venk/comics.
How can I get this to work?
P.s, I could just use AJAX to do this ./u/comics?username=Venk but that is just ugly and inconsistent and just overall makes my site's theme look messy so I'd much prefer an alternative.
P.p.s, Feel free the edit the post to make the title more concise, I wasn't really sure how to word it in basic terminology.
Create a .htaccess file in root with this content, make sure mod_rewrite is enabled.
You don't have to keep PHP files inside u directory, move profile.php and comics.php to root as well.
RewriteEngine On
# from: /u/{word}/comics
# to: /comics?username={word}
RewriteRule ^u/(\w+)/comics$ /comics.php?username=$1
When you edit a question on stackoverflow.com, you will be redirected to a URL like this:
https://stackoverflow.com/posts/1807421/edit
But usually, it should be
https://stackoverflow.com/posts/1807491/edit.php
or
https://stackoverflow.com/posts/edit.php?id=1807491
How was
https://stackoverflow.com/posts/1807421/edit
created?
I know that Stackoverflow.com was not created by using PHP, but I am wondering how to achieve this in PHP?
With apache and PHP, you might perform one of your examples using a mod_rewrite rule in your apache config as follows:
RewriteEngine On
RewriteRule ^/posts/(\d+)/edit /posts/edit.php?id=$1
This looks for URLs of the "clean" form, and then rewrites them so that they are internally redirected to a particular PHP script.
Quite often rules like this are used to route all requests into a common controller script, which might do something like instantiate a "PostsController" class and ask it to handle an edit request. This is a common feature of most PHP application frameworks.
It's called routing. Take a look at tutorials on the subject.
If you use a framework such as cake php it should be built in.
As #mr-euro stated you can use mod_rewrite but front controller is a far better solution.
You force every request to index.php and you write your application controlling in index.php.
You use Apache's .htaccess/mod_rewrite, and optionally a PHP file, which is the approach I like to take myself.
For the .htaccess, something like this:
RewriteEngine On
RewriteRule ^(.*)$ index.php
Then in your PHP file, you can do something like this:
The following should get everything after the first slash.
$url = $_SERVER['REQUEST_URI'];
You can then use explode to turn it into an array.
$split = explode('/', $url);
Now you can use the array to determine what to load:
if ($split[1] == 'home')
{
// display homepage
}
The array is starting from 1 since 0 will usually be empty.
It's indeed done by mod_rewrite, or with multiviews. But i prefer mod_rewrite.
First: you create a .htaccessfile with these contents:
RewriteEngine On
RewriteRule ^posts/([0-9])/(edit|delete)$ /index.php?page=posts&postId=$1&action=$2
Obvious, mod_rewrite must be enabled by your hostingprovider ;)
Using mod_rewrite this can be achieved very easily.
I am poor at this but i do know you can redirect urls using apache mod_rewrite and by touching config files. From what i remember htaccess can be used to redirect. Then internally when the user hits
http://stackoverflow.com/posts/1807421/edit it can use your page http://stackoverflow.com/edit.php?p=1807421 instead or whatever you want.
You could use htaccess + write an URI parser class.
I am relatively new to php. There is a very basic thing that has been troubling me. I understand that php is used to make web sites dynamic. I also understand that php is one of the many server side scripting languages that can be used to make dynamic web sites.
However, what I do not understand is that when do i need to use an index.php page. Say for example if i have just a simple login page on my index page, it could very well just be a simple html page as well. Right? Then why would i want to make it index.php instead of index.html?
An example of a sample situation would be great.
You will have to choose the PHP extension (.php) when you want php code to be executed in the file. PHP code is code between the opening <?php or <? and the closing ?> tags.
When no PHP code should be executed you can use the .html extension.
Usually when using the .php extension you are telling the web server, that it should use a php interpreter to process the file before it will be delivered to the browser. The php interpreter will then replace all content between the <?php and ?> by the output of the PHP code. Just as if you wrote it manually. The processed file will then be delivered to the browser.
However, using the .php extension to tell the web server to process php code is configurable. If you want you can use other file extensions too.
There is another thing that should be pointed out. When you only type the url path (without a filename) like :
http://www.myserver.com/
there is an order of extensions (filenames) which the webserver (apache) searches for an index document. For example an apache config may contain a section like:
<IfModule mod_dir.c>
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>
Meaning that the index document is searched in the order above. This means if you place an index.html and a index.php in the same folder - and having the configuration above - always the index.html would be delivered by the server.
It will not hurt a website if you serve every page as .php. Apache is very fast to serve any php, let alone one which contains only static html.
As a beginner you may find php will benefit you, by allowing you to creating simple templates. Site header and footer includes for instance could be written in one file, then included in all the other pages.
Its not a big deal whether you use index.php or index.html. You ca use anyone of either. Only thing is you need PHP(or any other server side scripting language) to make your site dynamic.
Like you have a login page,you can surely make it as inde.html but your logics would either have to be in a different file or embedded in HTMl.
You can use which-ever you prefer:
If you prefer keeping forms and basic pages that don't use data in HTML,
and keep pages that use php in php format that is fine.
But one method I and I assume most others use is just make all of your pages php files. This is because you can include a html document in the php file and display it just the same. But you cannot do php queries from a html file so it is easy to just always use php just incase you want to add some php scripts to it.
Index.php:
<?php
$var = 5;
?>
<html>
<head>
<title>My Page</title>
</head>
<body>
<h2>Your variable was <?php echo $var; ?></h2>
</body>
</html>
By default, the apache server needs a file with .php extension to parse the PHP code inside it. Though, if you wish, you may configure your server, by adding a single line to the configuration file, to use files with any extension with PHP code inside it. You can edit the apache by yourself to support php also in .HTML extension.
In simple terms, you can easily access index.html file and get the data beneath it.But index.php is difficult to access. For your simple application index.html will do the trick. If you are planning for some big and secure application go for index.php
It is beacuse sometimes you may have some logic written on index.php. Like you may check if user is logged in or not and then redirect user to some specific page. Also you may do device based redirection as in case of mobile devices.
You can always choose to create index.html but you do not know when youy may need to have some logic there.
For example, in my index.php or another main page I mainly use php because I use variables w/ them in the rest of my site:
<?php
$sitepath="http://www.example.com/public";
$author="your name here";
?>
Because I <?php echo $sitepath ?> every time I link an image in my website so it doesn't break or something. Since I'm reusing the name all the time, I use .php to be able to have that service, because if I use the name, I can change it globally.
I think that simple pages like 404.html, aboutus.html, or ViewOneBlogPost.html could be an HTML page, you might not need any functionality/variables for that.
To check current settings for file extension priority in apache2 with linux
/etc/apache2/mods-enabled/dir.conf
Well you can use HTML if you have a simple few pages where the code doesn't repeat itself a lot. But trust me, if you have a whole website in HTML with more than a thousand lines per file, you will want to use PHP. Why? Because of require and include. These will clean up your code when you need to use layouts, so you don't need to keep copying the hole code of your layout header, footer etc... It can be really difficult to maintain when you need to insert content and there's a lot of code in the way.
Here's some documentation about it
https://www.php.net/manual/en/function.include.php
How can I alter url or part of it using php? I lost the code that I have used and now I cannot find answer online.
I know using header('Location: www.site.com') gets redirect but how can I just show fake URL?
I don't want to use mod_rewrite now.
It is impossible in PHP, which is executed server side. Any change to the url you make will trigger a page loading.
I think it may be possible in javascript, but I really doubt this is a good idea, if you want to rewrite an url only in the user adressbar, you're doing something wrong, or bad ;)
What you've actually asked for isn't possible in using PHP (Although, in JavaScript you can use the dreadful hashbang or the poorly supported bleeding edge pushState).
However, in a comment on another answer you stated that your goal is actually friendly URIs without mod_rewrite. This isn't about showing a different URI to the real one, but about making a URI that isn't based on a simple set of files and directories.
This is usually achieved (in PHP-land) with mod_rewrite, so you should probably stick with that approach.
However, you can do it using ScriptAlias (assuming you use Apache, other webservers may have different approaches).
e.g. ScriptAlias /example /var/www/example.php in the Apache configuration.
Then in the PHP script you can read $_SERVER['REQUEST_URI'] to find out what is requested and pull in the appropriate content.
You can make somewhat SEO-friendly URLs by adding directories after the php script name so that your URLs become:
http://yoursite.com/script.php/arg1/arg2
In script.php:
<?php
$args = preg_split('#/#', $_SERVER['PATH_INFO']);
echo "arg1 = ".$args[1].", arg2 = ".$args[2]."\n";
?>
if you use some more htaccess trickery, then you can make the script.php look like something else (see #David's answer for an idea)
You can try using,
file_get_contents("https://website.com");
This is not going to redirect but fire the api and you can catch the output by assigning a variable to above function.
Hey guys, I've got a full Flash website that's deeplinked and I'm working on a php script that will dynamically generate the alternate content on the page.
I can't get the SEO version of swfaddress to work so I'm trying to go around another way.
This is how it works so far - my crawlable links are like this:
mydomain.com?id=video -----> is rewritten as: mydomain.com/video/
The php script reads the id and prepares the appropriate content. Problem is, when users click on the flash content, their urls appear like this:
mydomain.com/video/#contact,
mydomain.com/video/#about-us etc etc
Which is confusing. Is there a way to get rid of "/video/" in the uri path with PHP or javascript? Any ideas or not possible?
To anyone who may be in the same position as me; struggling to understand Asual's swfAddress SEO solution I've reached a satisfactory conclusion.
I've used a single index.php and supporting datasource.php which contains all results in a switch statement. The switch statement is called first in the index.php and runs through collecting the following info from an id (eg. ?id=video) sent via GET. The switch statement then continues to match up appropriate content, setting variables for the rest of the script to use. I've used the following:
$title
$description
$keywords
$canonical // hint: -> 'domain/video?id=somevid' to 'domain.com/video/' (see canonical in google)
$stylesheet
$body
After the datasource.php, I include a "head.php" below it, which gets the html rolling, doctype etc (but is mostly concerned with the < head >) and lays out the keywords, description and content, specific to that page.
Swfaddress javascript will set the deeplinking upon entry for Flash users:
SWFAddress.setValue(< ?php echo "/$id" ?>); // eg."/video"
The index.php uses swfobject to embed - I use static embedding (I heard somewhere it was better for SE, more future proof but who knows, I prefer it though). The alternate content is a div, which echos the stored $body extracted earlier.
Finally I use mod_rewrite to setup my crawlable links. During testing I'm referencing my files directly.
< IfModule mod_rewrite.c >
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^home/? ?id=home [NE,L]
RewriteRule ^about/? ?id=about [NE,L]
RewriteRule ^video/? ?id=video [NE,L]
< /ifModule >
This ensures my links look like this: http://www.mydomain.com/video/
Which results in: http://mydomain.com?id=video
A search engine can crawl it perfectly, viewing only the html content where as users will see the flash, and when they click on the links they will see something like this:
http://www.mydomain.com/video/#/contact
As I said it's satisfactory, not as ideal as Asual's SEO solution, but clean enough. I couldn't find ANY information on swfaddress' SEO sample anywhere, and I certainly don't know enough php to dive into the vast uncommented code.
Oh, be sure to include this in the head of your index.php, it will ensure your links don't screw up when your swfs load external files.
< base href="http://www.mydomain.com/" />
Hope that helps someone at some point.