<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>piouPiouM&#039;s dev&#187; Archives pour le tag URL – piouPiouM&#039;s dev</title>
	<atom:link href="http://pioupioum.fr/tag/url/feed/" rel="self" type="application/rss+xml" />
	<link>http://pioupioum.fr</link>
	<description>Bloc-note d&#039;un développeur web</description>
	<lastBuildDate>Thu, 29 Jul 2010 16:47:44 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP&#160;: valider qu&#8217;une URL pointe sur un domaine donné</title>
		<link>http://pioupioum.fr/snippets/php-valider-url-domaine.html</link>
		<comments>http://pioupioum.fr/snippets/php-valider-url-domaine.html#comments</comments>
		<pubDate>Sat, 05 Sep 2009 23:35:41 +0000</pubDate>
		<dc:creator>piouPiouM</dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[URL]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://pioupioum.fr/?p=236</guid>
		<description><![CDATA[Voici une petite fonction PHP pour vérifier qu&#8217;une URL pointe bien sur un domaine donné.

On valide dans un premier temps, via filter_var1, le format de l&#8217;URL. Cette dernière doit notamment être accompagné d&#8217;un protocole. La récupération de l&#8217;hôte depuis l&#8217;URL se fait très simplement avec la fonction parse_url. Enfin, on s&#8217;assure que le domaine cible [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start --><p>Voici une petite fonction PHP pour vérifier qu&#8217;une <abbr title="Uniform Resource Locator">URL</abbr> pointe bien sur un domaine donné.</p>

<p>On valide dans un premier temps, <em>via</em> <a href="http://www.php.net/manual/fr/function.filter-var.php" title="PHP: filter_var - Manual">filter_var</a><sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup>, le format de l&#8217;<abbr title="Uniform Resource Locator">URL</abbr>. Cette dernière doit notamment être accompagné d&#8217;un protocole. La récupération de l&#8217;hôte depuis l&#8217;<abbr title="Uniform Resource Locator">URL</abbr> se fait très simplement avec la fonction <a href="http://www.php.net/manual/fr/function.parse-url.php" title="PHP: parse_url - Manual">parse_url</a>. Enfin, on s&#8217;assure que le domaine cible est bien le domaine principal de l&#8217;hôte.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Checks a URL belongs to a given domain.
 *
 * @param string $url The URL to test.
 * @param string $domain The target domain.
 * @return boolean
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> isDomainOf<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #000088;">$domain</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span> <span style="color: #339933;">===</span> <span style="color: #990000;">filter_var</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span> FILTER_VALIDATE_URL<span style="color: #339933;">,</span> FILTER_FLAG_HOST_REQUIRED<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$host</span> <span style="color: #339933;">=</span> <span style="color: #990000;">parse_url</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span> PHP_URL_HOST<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$host</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>boolean<span style="color: #009900;">&#41;</span> <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#^(?:[^\.]*\.){0,}'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$domain</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'$#'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$host</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<p><span id="more-236"></span></p>

<h2>Suite de tests</h2>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$test</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'domain'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'pioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'url'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'http://pioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'synopsis'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Le domaine testé lui même.'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'domain'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'pioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'url'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'http://example.com'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'synopsis'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Un domaine différent.'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'domain'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'pioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'url'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'http://www.pioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'synopsis'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Un sous-domaine.'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'domain'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'pioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'url'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'http://foo.bar.pioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'synopsis'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Un double sous-domaine.'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'domain'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'bar.pioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'url'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'http://foo.bar.pioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'synopsis'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Le dommaine cible contient un sous-domaine.'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'domain'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'pioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'url'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'http://assets1.pioupioum.fr/foo/bar.png'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'synopsis'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Un contenu hébergé sur un sous-domaine.'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'domain'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'pioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'url'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'http://www.example.com/foo/bar.png'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'synopsis'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Un contenu hébergé sur un domaine différent.'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'domain'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'pioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'url'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'http://fakepioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'synopsis'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Un domaine proche.'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'domain'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'pioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'url'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'http://www.fakepioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'synopsis'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Un domaine proche avec un sous-domaine.'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'domain'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'pioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'url'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'pioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'synopsis'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Aucun protocol n\'étant spécifié, la fonction filter_var invalide le test.'</span>
    <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$test</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$c</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$ok</span> <span style="color: #339933;">=</span> isDomainOf<span style="color: #009900;">&#40;</span><span style="color: #000088;">$test</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$test</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'domain'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%s</span><span style="color: #000099; font-weight: bold;">\n</span>[<span style="color: #009933; font-weight: bold;">%s</span>] L'URL <span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #009933; font-weight: bold;">%s</span><span style="color: #000099; font-weight: bold;">\&quot;</span> <span style="color: #009933; font-weight: bold;">%s</span> sur le domaine <span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #009933; font-weight: bold;">%s</span><span style="color: #000099; font-weight: bold;">\&quot;</span>.<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #000088;">$test</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'synopsis'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ok</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'SUCCESS'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'FAIL'</span><span style="color: #339933;">,</span>
        <span style="color: #000088;">$test</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ok</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'pointe bien'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'ne pointe pas'</span><span style="color: #339933;">,</span>
        <span style="color: #000088;">$test</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'domain'</span><span style="color: #009900;">&#93;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<p>Qui nous donne l&#8217;affichage suivant&#160;:</p>


<div class="wp_syntax"><div class="code"><pre class="plain" style="font-family:monospace;">Le domaine testé lui même.
[SUCCESS] L'URL &quot;http://pioupioum.fr&quot; pointe bien sur le domaine &quot;pioupioum.fr&quot;.
&nbsp;
Un domaine différent.
[FAIL] L'URL &quot;http://example.com&quot; ne pointe pas sur le domaine &quot;pioupioum.fr&quot;.
&nbsp;
Un sous-domaine.
[SUCCESS] L'URL &quot;http://www.pioupioum.fr&quot; pointe bien sur le domaine &quot;pioupioum.fr&quot;.
&nbsp;
Un double sous-domaine.
[SUCCESS] L'URL &quot;http://foo.bar.pioupioum.fr&quot; pointe bien sur le domaine &quot;pioupioum.fr&quot;.
&nbsp;
Le dommaine cible contient un sous-domaine.
[SUCCESS] L'URL &quot;http://foo.bar.pioupioum.fr&quot; pointe bien sur le domaine &quot;bar.pioupioum.fr&quot;.
&nbsp;
Un contenu hébergé sur un sous-domaine.
[SUCCESS] L'URL &quot;http://assets1.pioupioum.fr/foo/bar.png&quot; pointe bien sur le domaine &quot;pioupioum.fr&quot;.
&nbsp;
Un contenu hébergé sur un domaine différent.
[FAIL] L'URL &quot;http://www.example.com/foo/bar.png&quot; ne pointe pas sur le domaine &quot;pioupioum.fr&quot;.
&nbsp;
Un domaine proche.
[FAIL] L'URL &quot;http://fakepioupioum.fr&quot; ne pointe pas sur le domaine &quot;pioupioum.fr&quot;.
&nbsp;
Un domaine proche avec un sous-domaine.
[FAIL] L'URL &quot;http://www.fakepioupioum.fr&quot; ne pointe pas sur le domaine &quot;pioupioum.fr&quot;.
&nbsp;
Aucun protocol n'étant spécifié, la fonction filter_var invalide le test.
[FAIL] L'URL &quot;pioupioum.fr&quot; ne pointe pas sur le domaine &quot;pioupioum.fr&quot;.</pre></div></div>


<h3 class='related_post_title'>Continuez votre lecture sur des sujets similaires</h3>

<ul class='related_post'><li><a href='http://pioupioum.fr/developpement/jslint-console-rhino.html' title='Une console JSLint pour aider vos validations JavaScript'>Une console JSLint pour aider vos validations JavaScript</a></li><li><a href='http://pioupioum.fr/snippets/textmate-commande-exporter-mots-word-wrap.html' title='TextMate : commande &laquo;&nbsp;Export words&nbsp;&raquo;'>TextMate&#160;: commande &laquo;&nbsp;Export words&nbsp;&raquo;</a></li><li><a href='http://pioupioum.fr/snippets/php-installer-apc-macosx.html' title='PHP : installer APC sous Mac OS X Leopard'>PHP&#160;: installer APC sous Mac OS X Leopard</a></li><li><a href='http://pioupioum.fr/snippets/php-convertir-datetime-unix-timestamp.html' title='PHP : convertir un DATETIME en un timestamp UNIX'>PHP&#160;: convertir un DATETIME en un timestamp UNIX</a></li><li><a href='http://pioupioum.fr/snippets/php-uncamel-fonction-convertir-camel-case.html' title='PHP : fonction uncamel() pour inverser une notation en CamelCase'>PHP&#160;: fonction uncamel() pour inverser une notation en CamelCase</a></li></ul>

<div class="footnotes">
<hr />
<ol>

<li id="fn:1">
<p>la fonction requiers au minimum PHP 5.2.0.&#160;<a href="#fnref:1" rev="footnote">&#8617;</a></p>
</li>

</ol>
</div>
<!-- google_ad_section_end -->]]></content:encoded>
			<wfw:commentRss>http://pioupioum.fr/snippets/php-valider-url-domaine.html/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
