Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to reference php from within html?

Status
Not open for further replies.

RenoWV

Technical User
Mar 16, 2002
156
US
Is there a way to reference a php script from within an existing html page, in a similar manner to how external javascripts are referenced?

For example, I'd like to utilize a php script that will detect a person's ip and redirect them to another page. I want to put this on my mail form html page because one particular person is using the mailform to send me spam. I found a php script that says it will do that, and in the instruction it says to include on the page to be protected this line of code:

<?php include('redirect.php'); ?>

Am I correct in saying that I cannot just drop that onto an html page? If I can't, I'm wondering if anyone has seen a method that will work -- for example, using js "document.write" or some other technique??

Any advice is appreciated...
 
You can drop it in, but you need to rename your HTML source as somefile.php

Also, your web server needs to be set up to process PHP files.

If any other HTML files reference the renamed file, either make a dummy HTML file with the original name and use the <META> redirect tag to redirect to the new PHP file or, if your web server is Apache, you can do this redirect in a .htaccess file.

Ken
 
Thanks Ken for your detailed advice. If I were to simply rename the file from "contact_form.html" to "contact_form.php", would I also need to add to the very top:
<?php
and to the very bottom:
?>

I should also point out that because this is a mail form, it is referencing a cgi script, as in:
Code:
<FORM ACTION = "cgi-bin/Contact_Mail.pl" METHOD = "POST" >

So will "Contact_Mail.pl" still execute if it's contained within a php file, rather than the original html?

 
No you don't have to add '<?PHP' or '?>'. In fact if you add those tags it will break your code. You only put the tags around PHP code to tell the interpreter to start looking at the PHP code.

Once PHP gets done with the file, the web server sees HTML, so everything that worked before should still work.

Ken
 
Thanks again Ken for your instruction. I was hoping (incorrectly as it turns out) to find that php allowed simple external referencing the same way that js or shtml does, as in
Code:
<script language="JavaScript1.2" src="scripts/script-name.js"></script>
 ...or...
<!--#exec cgi="cgi-bin/script-name.cgi"-->

Oh well -- can't have it all. Many thanks for clearing up my misconception (one of many I harbor!)...

-------------------------
 
You can just put
Code:
<?php include('redirect.php'); ?>
in your page once you rename it to contact_form.php. Was that the misconception, or some other one? [smile]

Greg
"Personally, I am always ready to learn, although I do not always like being taught." - Winston Churchill
 

"Was that the misconception, or some other one?"

Don't get me started Greg with how much I *don't* know!

This particular misconception was that I'd be able to reference the php without renaming a page, as I can do for example when I want to use js.

But since my reply to Ken, I had another question, so perhaps one of you can help --

If I want to include an ssi directive on an html page, it's my understanding that I can use htaccess to permit that to work. Can I do the same with php & htaccess? As in:
Code:
<Files foo.html>
AddHandler server-parsed .php
</Files>
 
Well RenoWV,

You CAN put <?php include("some_file.php"); ?> in a regular HTML file.

The only catch is - your webserver needs to be setup so that the php parse also parses html files. This is not usually set by default.
 
You CAN put <?php include("some_file.php"); ?> in a regular HTML file.... The only catch is..."

Very good news Borvik -- I'll ask the tech guys at our ISP if that is something they can set up. If that's all it takes, then we are home free. You have all been very helpful -- thanks much...

---------------
 
Better still, if you can use .htaccess include a mod-rewrite rule to redirect the "old" .html page to the "new" .php version.

That way you can change the name of your page AND still keep any links to the .html page.

Parsing ALL pages to the PHP parser is unnecessary load on your server. I doubt your ISP will do it.

Foamcow Heavy Industries - Web design and ranting
Toccoa Games - Day of Defeat gaming community
Target Marketing Communications - Advertising, Direct Marketing and Public Relations
"I'm making time
 
Thanks Foamcow. Would that .htaccess "mod-rewrite rule" be structured as I posted above?
Code:
<Files foo.html>
AddHandler server-parsed .php
</Files>
If not, could you suggest the proper format that should be used in this situation?

--------------------------
 
I'm not an expert, your best bet is to google or ask in the Apache forum but here's my attempt...

In you r .htaccess file add this

Code:
RewriteEngine On
RewriteRule ^oldPage$ newPage.php

I think that will redirect oldPage.<any extension> to newPage.php

I guess you could be more specific and do something like

Code:
RewriteEnging On
RewriteRule myPage.html myPage.php

Like I said, I don't know if that is exactly right but it may well work fine for you.
Note: it won't work if modRewrite is not available in your server setup.

Foamcow Heavy Industries - Web design and ranting
Toccoa Games - Day of Defeat gaming community
Target Marketing Communications - Advertising, Direct Marketing and Public Relations
"I'm making time
 

Thank you Foamcow for the input. Let's see if there are any Apache/htaccess people following this thread, and if so, perhaps they'll be kind enough to comment one way or the other about your modRewrite coding.

If it's correct then we'll go with it; if not, I hope someone will offer the format which is most effective when using a "php include" inside an existing html page....

----------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top