I just meet an important problem as I have a lot of sources from another domain which means if I use iframe, I have to resize all of these contents. But the problem is that I can not modify or insert code to source web page.
Because of these, I would like to ask if there are any other solutions to skip useing iframe?
Tips: I need the entire contents (include images, css and so on) from the url. Not part of the contents.
Quick idea - you could try to write a proxy on server side and serve the content in the iframe, but with src pointing to the proxy page instead of the real page (i.e. controlled with passed parameters)
PHP's include function might be the best place to start. Is it just the markup you're unable to change, or are you able to use PHP and JavaScript?
EDIT:
Try using include('http://www.google.com') to include a URL in your page without using an iFrame. Any non-absolute directory references in the code (like <img src='/img.png'> will not display or load correctly.
If you need to fix up these references and don't have the ability to change the markup itself, you can use the file_get_contents function and modify things like this:
$page = file_get_contents('http://www.google.com');
$page = preg_replace('/(href|src)=([\'"])\//',"$1=$2http://google.com/",$page);
echo $page;
Make an AJAX call using the JSONP data type to return the contents of the other page. You would need to make modifications to the code on both domains though to make this work. http://en.wikipedia.org/wiki/JSONP
Modified answer of #beanland, my /proxy.php file, with caching:
$host = parse_url($_GET['url'], PHP_URL_HOST);
$dir = $_SERVER[DOCUMENT_ROOT].'/cache_proxy/'.$host;
if(!is_dir($dir))
mkdir($dir);
$filepath = $dir.'/'.md5($_GET['url']);
if(is_file($filepath)){
include($filepath);
}else{
$page = file_get_contents($_GET['url']);
$page = preg_replace('/(a href)=[\'\"](http.*)[\'\"]/', '$1="http://buy/proxy.php?url=$2"', $page);
$page = preg_replace('/(a href)=[\'"][^http](.*)[\'"]/', '$1="http://buy/proxy.php?url=http://'.$host.'/$2"', $page);
$page = preg_replace('/(href|src)=[\'"][^http+](.*)[\'"]/', '$1="http://'.$host.'/$2"', $page);
file_put_contents($filepath, $page);
echo $page;
}
First replace all <a href=""> links to your proxy, then replace all relative <img src="/path..."> etc. to absolute <img src="http://...">
jQuery Load into a div http://api.jquery.com/load/
Related
I would like to create a single URL that returns one image when loaded in a page within my domain, and another slightly modified image when loaded in a page outside my domain.
I am thinking along the lines of something like:
<?php
header('Content-type: image/jpg');
if (/**image is loaded within my domain**/)
{
readfile("image1.jpg");
}
else
{
readfile("image2.jpg");
}
?>
Is there something I can put in the if-statement to make it work? Possibly that works in all browsers?
Is there a way to do this without php?
You could use the referrer URL in the request and check to see if it is your domain. This is done using $_SERVER['HTTP_REFERER'].
However, the HTTP_REFERER URL can be easily modified by clients and can even sometimes not be set, so you need to be careful when using it.
I don't know, how you're going to get request for image not in your domain, but you may look at $_SERVER['HTTP_HOST']. Maybe, HTTP_REFERER is what you need. Anyway look here.
I have a website that was written assuming http:// is one and only protocol forever. Now i bought a SSL certificate but when i visit site calling it with https:// i get info in browsers that part of site is insecure. As i found i have some JS, CSS and images and files that i refer to using http:// in the HTML of the site.
So what is best practice to enable full https? Should i change my website in every place when i refer to image, CSS or JS, check if site was loaded with http or https and load the resource with according protocol? It seems like a lot of work for me and bit error prone. Is there any other way, easier to make the whole site fully secure?
Rather than linking to your css, js, and images with http://yoursite.com/css/file.css just use relative paths such as /images/image.jpg and /css/file.css this way it will work with both http and https, also if you change domains or copy your content to another domain, you shouldn't have to change all those links either.
Use relative paths. If you are pointing to something that is on the same site as yours, then you should not be using http://
If for some reason you still need to have http:// then just switch them all to https://. An http:// will never complain because it is pointing to https:// stuff, but an https:// page will complain if it is pointing to non-https stuff.
If you are pointing to content outside of your control, on another site for example, then you need to hope that you can get at that content via https instead. If you can't, then you're hosed and you either need to live with the error, get the content from somewhere else, or proxy the content through your own https connection.
To complement #drew010 's answer, you could use other domains and still refer to the current protocol with //, something like:
<img src="/pics/home.png" />
<img src="//my-cdn.com/pics/info.png" />
The latter example will point to https://.. from https://your-site.com and http://... from http://your-site.com.
the best practice would be either using relative path rather than absolute but sometimes absolute is a better option so you can do the following :
as I can imagine you have a file called config.php or common.php (a file that stores your common used vars and you include it in every page), so put this code there :
function selfURL() {
$s = empty($_SERVER["HTTPS"]) ? ''
: ($_SERVER["HTTPS"] == "on") ? "s" : "";
$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
}
function strleft($s1, $s2) {
return substr($s1, 0, strpos($s1, $s2));
}
and then you can assign a var called $http to get the value of the function like :
$http = selfURL();
and then whenever you want to include anything like images, css, etc do something like :
<img src="<?=$http?>images/sample.png" />
this method is reliable as it works in any situation.
Using PHP, is there a way to test if the browser is accessing a certain page?
For example, I have a header file called header.php which is being pulled into a couple different pages. What I want to do is when I go to a different page, I want to append certain variable to the title.
Example.
Inside header.php:
<?php
$titleA = " Online Instruction";
$title B = "Offline";
?>
<h2>Copyright Info: <?php if ('onlineinstruction'.php) echo $titleA; ?> </h2>
edit: also if you believe there is a simpler way to do this, let me know!
You can use $_SERVER['REQUEST_URI'], $_SERVER['PHP_SELF'], or __FILE__ depending on your version of PHP and how you have your code setup. If you are in a framework it may have a much more developer-friendly function available. For example, CodeIgniter has a function called current_url()
Per PHP Docs:
$_SERVER['REQUEST_URI']: The URI which was given in order to access
this page; for instance, '/index.html'.
$_SERVER['PHP_SELF']: The filename of the currently executing script,
relative to the document root. For instance, $_SERVER['PHP_SELF'] in a
script at the address http://example.com/test.php/foo.bar would be
/test.php/foo.bar. The __ FILE__ constant contains the full path and
filename of the current (i.e. included) file. If PHP is running as a
command-line processor this variable contains the script name since
PHP 4.3.0. Previously it was not available.
<?php
$url = $_SERVER["REQUEST_URI"];
$pos = strrpos($url, "hello.php");
if($pos != false) {
echo "found it at " . $pos;
}
?>
http://www.php.net/manual/en/reserved.variables.server.php
http://php.net/manual/en/function.strrpos.php
You can use this variable to find out what page you're on:
$_SERVER['PHP_SELF']
http://www.php.net/manual/en/reserved.variables.server.php
read this: http://php.net/manual/en/language.constants.predefined.php
Any php page can include the following code, putting the applicable page title in the variable.
<?php
//Set Page Title for header template
$page_title = 'Welcome';
require_once("templates/header.php");
?>
In the header template:
<title><?php echo $pageTitle ?></title>
Any page called up will show your preferred title in the browser tab, and be usable as a variable on the specific page.
How can include CodeIgniter content in a regular PHP page on the same server but not part of the CI app?
For example I'm am trying to load a header from CI into Wordpress. Whats the best way to include a CI controller (eg; index.php/mycontroller/header/) on the same server?
From http://codeigniter.com/forums/viewthread/88635/
This is overkill, as file_get_contents($url) or similar, would be better. However, it may work for your situation:
$CI_INDEX = '/path/to/your/codeigniter/index.php';
$path = '/controller/method';
$_SERVER['PATH_INFO'] = $path;
$_SERVER['REQUEST_URI'] = $path;
chdir(dirname($CI_INDEX));
ob_start();
require($CI_INDEX);
$output = ob_get_contents();
ob_end_clean();
die($output);
Use PHP's built-in file_get_contents(). Just make sure to use the full HTTP path, not a relative path. Example:
<?php
file_get_contents('http://your.server.com/codeigniter-path/controller/');
That should do the trick.
Have you considered using an iframe in your Wordpress page?
<iframe src="http://my-site-url/index.php/mycontroller/header"></iframe>
It might be the simplest solution.
When you are trying to wrap content with HTML from an external source, this can easily be achieved by placing HTML comments (or other recognizable tags) in the target site, then using PHP to split/explode the content.
I have used this method to create several micro-sites for MSN Money which has in. Then I would simply use:
list($header_html) = explode('<!-- Header -->', file_get_contents($url));
It was slightly more complex than that, involving caching and all sorts of other madness, but at its base that is the method to use if WordPress will allow it.
I would like to create a basic URL rewrite using frames.
I don't have access to .htaccess to do mod_rewrite.
Is there a way using PHP, jQuery, JavaScript etc. to detect which URL has been clicked on and then open URL in new frame?
Ex: user clicks on /index.php?12345 it will open in framed window /pages/12345/index.html and if they click on /index.php?54321 URL will open in framed window /pages/54321/index.html
I don't think I really understand what you mean. Usually url rewrite works like this:
User clicks on http://example.com/content/example
Which is the rewritten to http://example.com/index.php?cat=content&page=example
You can somewhat fake this effect by making your links into http://example.com/index.php/content/example the webserver will still request the page index.php, in which you can then read the part after index.php (but before a query string) with
$_SERVER['PATH_INFO']
and then parse that to get what you need.
PHP.net on $_SERVER['PATH_INFO']
Contains any client-provided pathname
information trailing the actual script
filename but preceding the query
string, if available. For instance, if
the current script was accessed via
the URL
http://www.example.com/php/path_info.php/some/stuff?foo=bar,
then $_SERVER['PATH_INFO'] would
contain /some/stuff.
A PHP solution would be something along these lines:
if (!empty($_REQUEST)) {
$page = array_pop($_REQUEST);
if (is_numeric($page)) {
header("Location: pages/$page/index.html");
exit;
}
}
If I've really understand what you want, I think it's easy.
When the user clicks it calls a JQuery function which sends the content of the link to PHP with AJAX. After that, PHP analyses the link, gets the content of the page (with include()) and sends that to JQuery via JSON.
Such jquery might help,
jQuery(function($){
//this prevents all link elments' default behaviour
//when you click they won't navigate the page
$("a").click(function(){
//you get the links href
var actual_url = $(this).attr("href");
var new_url = "";
//[write your code here for converting actual url to new one]
//and open the new url in the frame you want
window.parent.yourFrameName.location = new_url;
//necessary for preventing default behaviour
return false;
});
});
Sinan.
Best solution is to use jquery to check if link was visited and then change link's target to _blank.
You could use plugin from this site:
link text and then execute such code:
$('a').visited(function () {
$(this).attr('target','_blank');
});
I think it is what are you looking for.