0

what I would like to do is store the HTML of a site in a php variable so it can be echoed at a certain condition. My PHP code:

<?php
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$URL = "http://www.example.com/";
$URL2 = "http://www.example2.com/";
$website1 = ?
$website2 = ?
if($url == $URL) {
    echo $website1;
} else if($url == $URL2) {
    echo $website2;
}
?>

I want this HTML (below) to be stored in $website1
There will also be HTML stored in $website2

<html>
    <head>
        <link rel='stylesheet' type='text/css' media='all' href='css/style.css'>
        <link rel='stylesheet' type='text/css' media='all' href='css/jquery-ui-1.10.4.custom.css'>
        <title>Website #One</title>
        <script type='text/javascript' src='js/jquery-1.9.1.js'></script>
        <script type='text/javascript' src='js/jquery-ui-1.10.4.custom.js'></script>
        <script type='text/javascript' src='js/script.js'></script>
    </head>
    <body>
    <div id='header'></div>
    <div id='menu'>
        <button id='navbtn' class='active btn'>Home</button><button id='navbtn2' class='btn'>Pictures</button><button id='navbtn3' class='btn'>Videos</button><button id='navbtn4' class='btn'>Games</button><button id='navbtn4' class='btn'>About</button><button id='navbtn5' class='btn'>Contact</button>
    </div>
    <div id='content'></div>




    </body>
</html>

How would I be able to achieve this?

1
  • You can use file_get_contents pointing to html file. Commented Oct 29, 2014 at 11:31

5 Answers 5

2

I don't understand what logic you are using, but you can achieve this using the PHP's built in function file_get_contents(). Use this way:

<?php
    $url = "http://".getenv("HTTP_HOST")."/".getenv("REQUEST_URI");
    $URL = "http://www.example.com/";
    $URL2 = "http://www.example2.com/";
    $website1 = file_get_contents($URL);
    $website2 = file_get_contents($URL2);
    if($url == $URL) {
        echo $website1;
    } else if($url == $URL2) {
        echo $website2;
    }
?>
Sign up to request clarification or add additional context in comments.

2 Comments

I would add a note regarding allow-url-fopen
@ʰᵈˑWhat's that? What should I add? Feel free to edit my post! Thanks in advance.
0

You can use file_get_contents(); Or the CURL library http://php.net/manual/fr/book.curl.php

3 Comments

hi thanks for quick reply but can you give me an example code because I am not sure what you mean
You must learn to read a documentation or you will never be a good programmer. This is a really easy function.
$website1 = file_get_contents("website1.com"); $website2 = file_get_contents("website2.com");
0

Curl is a nice way to do this: http://www.digimantra.com/technology/php/get-data-from-a-url-using-curl-php/

<?php
$url='http://twitter.com/statuses/user_timeline/16387631.json'; //rss link for the twitter timeline
print_r(get_data($url)); //dumps the content, you can manipulate as you wish to

/* gets the data from a URL */

function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>

Comments

0

Thanks guys I was able to learn from your answers and managed to get it to work here is my final php code:

<?php
$get_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$url1 = "http://thedag.ga/thedagga/main.php";
$url2 = "http://thedag.ga/zp/zp18.php";
$tdurl1 = "http://thedag.ga/";
$tdurl2 = "http://www.thedag.ga/";
$zpurl1 = "http://zp.thedag.ga/";
$zpurl2 = "http://zp18.thedag.ga/";
$zpurl3 = "http://zp4rker.thedag.ga/";
if($get_url == $tdurl1 || $get_url == $tdurl2) {
    print_r(file_get_contents($url1));
} else if($get_url == $zpurl1 || $get_url == $zpurl2 || $get_url == $zpurl3) {
    print_r(file_get_contents($url2));
}
?>

Comments

-1

Why not just put both versions in there own PHP file, that is, if it is only some simple one page html, and not an entire website, then the other suggestions would probably be better.

<?php
$url = "http://".getenv("HTTP_HOST")."/".getenv("REQUEST_URI");
$URL = "http://www.example.com/";
$URL2 = "http://www.example2.com/";
if($url == $URL) {
    include 'header1.php';
} else if($url == $URL2) {
    include 'header2.php';
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.