Reputation: 21
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?
Upvotes: 0
Views: 756
Reputation: 21
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));
}
?>
Upvotes: 0
Reputation: 16
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';
}
?>
Upvotes: -1
Reputation: 167230
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;
}
?>
Upvotes: 2
Reputation:
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;
}
?>
Upvotes: 0
Reputation: 2679
You can use file_get_contents();
Or the CURL library
http://php.net/manual/fr/book.curl.php
Upvotes: 0