2

Code:

<input type="text" id="url_value"/>
<input type="button" id="get_title" value="Get title"/><br/>

<span class="url_title"></span>

Fiddle: http://jsfiddle.net/AdNgZ/

Is there a way to put any url on the text input, get the page title and show it on the span without going to that page?

Example: If I put the url of this page: Get external page title from url, I get the page title: php - Get external page title from url

Community
  • 1
  • 1
Notsogood13
  • 111
  • 1
  • 3
  • 11
  • 3
    It's called `cURL` my friend: http://stackoverflow.com/questions/3711357/get-title-and-meta-tags-of-external-site – chriz Mar 26 '13 at 15:02
  • 3
    JS cannot directly reach out to external sites to retrieve content. That's a violation of the same origin security policy. You can, however, have your server fetch the page title and send it over to the JS via ajax. – Marc B Mar 26 '13 at 15:02
  • You forgot **- Stack Overflow** there. – hjpotter92 Mar 26 '13 at 15:04
  • @chriz, thank you! And sorry for repost =/ – Notsogood13 Mar 26 '13 at 15:07

1 Answers1

0
<?php
$page = file_get_contents($_REQUEST["url"]);
$title = preg_replace('/<title>(.*)</title>/i', '$1', $page);
echo htmlentities($title);
?>

With this code you need to activate allow_url_fopen php.ini directive.

WARNING: try to make a list of secure website and allow to get title only in their pages, to avoid XXS attacks.

pietroalbini
  • 336
  • 3
  • 13
  • http://security.stackexchange.com/questions/42498/best-way-to-sanitize-user-input-in-php – Leo Jan 26 '15 at 00:35