Jump to content

  • Log in with Facebook Log in with Twitter Log In with Google      Sign In   
  • Create Account

Subscribe to HRA Now!

 



Are you a Google Analytics enthusiast?

Share and download Custom Google Analytics Reports, dashboards and advanced segments--for FREE! 

 



 

 www.CustomReportSharing.com 

From the folks who brought you High Rankings!


Sponsored Content

 

 
 

Photo
- - - - -

What's The Best Include Method?


  • Please log in to reply
24 replies to this topic

#16 chrishirst

chrishirst

    A not so moderate moderator.

  • Moderator
  • 5,882 posts
  • Location:Blackpool UK

Posted 28 November 2004 - 11:24 AM

for including content from your site on another without scripting you are a bit limited to IFrames or going down the RSS route and javascript, neither of which are any good for SEO.

With asp you can use the XMLHTTP object to grab the page code from a URI on your site and display it remotely

CODE
<%
const strUserAgent =  "PageGrab Bot"

function GetPageCode(strURL)
Response.Buffer = True
 Dim objXMLHTTP, xml
 Set xml = Server.CreateObject("Microsoft.XMLHTTP")
 
 xml.Open "GET", strURL, False
    xml.setRequestHeader "User-Agent", strUserAgent
    xml.Send
    if xml.status = 200 then
    GetPageCode = xml.responseText
    else
    GetPageCode = cstr(xml.status) & " (" & xml.StatusText & ")"
    end if
 Set xml = Nothing
end function
%>


To use this it's simply
CODE
<%
response.write GetPageCode("http://www.domain.com/page.htm")
%>

on the page where the content has to appear. Obviously be aware of having <html><head> sections and any javascript in the page which may or may not cause errors. But it's not a massive task to write a function to strip out blocks of code that may cause problems
With a bit more thought you can also recode the function to pull your CSS file and insert it into the remote page as inpage styles.

You can do similar in PHP with file_get_content() though haven't got a PHP version coded up and tested.

#17 MrLeN

MrLeN

    HR 2

  • Active Members
  • PipPip
  • 33 posts

Posted 28 November 2004 - 11:30 AM

Ok, that proves it. There is no easy way to do what I need lol.gif

..but thanks anyway.

I'm just going to have to update the templates manually as I get the time. I was thinking about making a "simplified" template, which just has the site logo and a few special links, but I want search engines to crawl through all the links.

I see links as arteries on a highway. The more links there are, the easier search engines can get from 'state to state' (site to site).

Bah! I'll just keep doing what I've been doing.

MrLeN

#18 Scottie

Scottie

    Psycho Mom

  • Admin
  • 6,293 posts
  • Location:Columbia, SC

Posted 28 November 2004 - 07:18 PM

Hosting is cheap.

Assuming these are all your owned sites, move them to a php enabled server.

If they aren't your sites, one wonders why you would be including other people's content (server by their server) in your pages....

#19 pkg123

pkg123

    HR 1

  • Members
  • Pip
  • 2 posts

Posted 19 December 2004 - 05:20 PM

This solved my initial problem of how I can include a php file from a remote server, ie, include('http://www.someother...includefile.php');

All seems pretty straight forward smile.gif

BUT, what I've found is the include file ignores any variables that have been set in the original php script. mad.gif

Now, I managed to find a way round this by posting the variable with the url for the include file as follows:

include ('http://www.someothersite.com/includefile.php?myvar='.$myvar);

This gets '$myvar' into the includefile.php and does what its supposd to do, but then similar to the problem in getting the $myvar into the includefile.php, I don't know how to get the resulting values back into the main php script to be used.

This all seems a bit wierd since I thought include files slotted seamlessly into the main php scripts, utilising existing variables and passing on new results.

Any suggestions??

#20 chrishirst

chrishirst

    A not so moderate moderator.

  • Moderator
  • 5,882 posts
  • Location:Blackpool UK

Posted 19 December 2004 - 06:49 PM

welcome to HR pkg23

includes only insert seamlessly when they exist on the same server.

The problems you are seeing is how variables are defined. they do not exist in a global context but in the memory space of each application instance, and are therefore not accessible outside of the server session.
So when the remote script runs locally the variables will be redefined in the local server session.

#21 pkg123

pkg123

    HR 1

  • Members
  • Pip
  • 2 posts

Posted 19 December 2004 - 08:48 PM

Thanks for your explaination chrishirst...

As you saw, I managed to get my variable into the remote include file by posting it within the include file url.

Is there any way of getting the processed results variables from the remote include file back to the original php script so it can finish it's execution?

I tried putting the script as a function within the remote include and calling it from the original script but it bombed out saying no such function etc...

Any thoughts?? crossfingers.gif

#22 Raphael

Raphael

    The Limey Cowboy

  • Active Members
  • PipPipPipPipPipPip
  • 722 posts
  • Location:New England

Posted 12 January 2005 - 09:19 AM

QUOTE(MrLeN @ Nov 28 2004, 03:25 AM)
Like this?

www.newezone.com/assorted/test.html

It doesn't seem to work.

MrLeN
View Post



In this example, your server isn't parsing the PHP code. Make sure you've got it set to push .html files through the PHP interpreter (lots of posts around here on how to do that in Apache, I have no idea about IIS - besides, Apache's better and quite literally infinitely cheaper)

Cross-server includes have been in PHP since the later versions of PHP 3, I believe. I'm pretty sure they weren't in PHP/FI. That said, maybe they weren't available until PHP4. I wouldn't upgrade to PHP5 yet, personally.

This from php.net:

QUOTE
Windows versions of PHP prior to PHP 4.3.0 do not support accessing remote files via this function, even if allow_url_fopen is enabled.


As for including asp pages.. I've never had to do that. How I'd do it, I think, would depend on how the cross-server include works. I've never used the cross-server capability of include, but php.net has this to say:

QUOTE
If "URL fopen wrappers" are enabled in PHP (which they are in the default configuration), you can specify the file to be included using a URL (via HTTP or other supported wrapper - see Appendix L for a list of protocols) instead of a local pathname. If the target server interprets the target file as PHP code, variables may be passed to the included file using a URL request string as used with HTTP GET. This is not strictly speaking the same thing as including the file and having it inherit the parent file's variable scope; the script is actually being run on the remote server and the result is then being included into the local script.


If I'm interpreting this correctly, in plain english it means:

When you're including a file from a remote server, the remote server will parse the file and then send you it's output.

That means you don't have to parse the file yourself. So you should be able to go ahead and include .asp pages with a regular PHP include call, i.e.:

include ("http://www.someserve...m/somefile.asp")

And you'll get regular HTML code back, which your PHP script can just print or echo to the browser.

Also, it would seem from the documentation that you can pass remotely-included files variables, so you could do something like:

include ("http://www.someserver.com/somefile.asp?var=$somevalue")

And the remote server will process that normally. I'm guessing that the HTTP_REFERER (sic) given to the remote server would be the address of the PHP script on your server that's doing the includes.

Anyway, the upshot of all this geek-talk is this:

1) ensure your web server is parsing .html files as PHP scripts. (this is pretty important as without it, you'll get somewhat less scripting achieved than you want wink.gif )

2) You can include asp, html and php files remotely with PHP's [b]include[b] statement and all you'll get back is regular HTML.

3) Because of (2) above, you cannot (I think) execute a script written remotely on your server. Depending on what your needs are, this may or may not be important.

Sooooo.. I hope that was of some help =)

Edited by BobetteKyle, 12 January 2005 - 05:08 PM.


#23 Raphael

Raphael

    The Limey Cowboy

  • Active Members
  • PipPipPipPipPipPip
  • 722 posts
  • Location:New England

Posted 12 January 2005 - 09:30 AM

Sorry, just noticed these other posts. I tell ya, don't read forums when you're not yet awake..

Anyway, about the result of the remote script:

Again, from php.net (All your answers are here, young padawan)

the include statement returns 1 of 2 things. It either returns 1 on sucessful or NULL on failure (and a warning is issued)

OR

It returns what it finds as the result of the script.

As an example:

if we say this is the local file
CODE
mainfile.html

<?
$result = include ("otherfile.php");
echo $result;
?>


remote file 1:
CODE
otherfile.php

<?
$var = "Hello, world";
?>


remote file 2:
CODE
otherfile.php

<?
$var = "Hello, world";
return $var;
?>


If we include remote file 1, we get a result of 1 back, because we don't pass anything back to the calling script. However, if we include remote file 2, $result will contain "Hello, world", because we passed it back via the return comamnd.

So, make sure you return your final data at the end of your included file (or wherever in your included file you want to) and you should be fine =)

#24 sherri

sherri

    Perceptum et Invenio

  • Active Members
  • PipPipPip
  • 89 posts
  • Location:Lost in Canada

Posted 14 January 2005 - 02:56 PM

You can't TECHNICALLY include a php file from another server. The calling script will not get the php source of the included file. It will get the generated html only.

Otherwise that would be a major security problem.

#25 Raphael

Raphael

    The Limey Cowboy

  • Active Members
  • PipPipPipPipPipPip
  • 722 posts
  • Location:New England

Posted 14 January 2005 - 03:24 PM

Tehcnically, yes.. but the command within php is the same :-D




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users