<?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>Sir Bit</title>
	<atom:link href="http://www.manuelmarangoni.it/sir-bit/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.manuelmarangoni.it/sir-bit</link>
	<description>Curiosità e approfondimento sul web e sull&#039;informatica</description>
	<lastBuildDate>Sun, 09 Dec 2018 14:04:56 +0000</lastBuildDate>
	<language>it-IT</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>Php, html, htaccess &#8211; Velocizzare il sito &#8211; Accorgimenti, caching e compressione delle pagine</title>
		<link>https://www.manuelmarangoni.it/sir-bit/2003/php-html-velocizzare-il-sito-accorgimenti-caching-e-compressione-delle-pagine/</link>
					<comments>https://www.manuelmarangoni.it/sir-bit/2003/php-html-velocizzare-il-sito-accorgimenti-caching-e-compressione-delle-pagine/#respond</comments>
		
		<dc:creator><![CDATA[Manuel Marangoni]]></dc:creator>
		<pubDate>Thu, 20 Mar 2014 10:51:41 +0000</pubDate>
				<category><![CDATA[Programmazione generale]]></category>
		<category><![CDATA[archivi]]></category>
		<category><![CDATA[htaccess]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[php]]></category>
		<guid isPermaLink="false">http://www.manuelmarangoni.it/sir-bit/?p=2003</guid>

					<description><![CDATA[<p>Una serie di accorgimenti per ottimizzare il codice. Vediamo inoltre come abilitare il sistema di caching e di compressione per le pagine di un sito con gzip.</p>
The post <a href="https://www.manuelmarangoni.it/sir-bit/2003/php-html-velocizzare-il-sito-accorgimenti-caching-e-compressione-delle-pagine/">Php, html, htaccess – Velocizzare il sito – Accorgimenti, caching e compressione delle pagine</a> first appeared on <a href="https://www.manuelmarangoni.it/sir-bit">Sir Bit</a>.]]></description>
										<content:encoded><![CDATA[<p>Avete costruito il vostro sito in ogni sua parte. Con un po&#8217; di tempo e di esperienza, siete anche riusciti a <strong>ottimizzare il codice php e le query</strong> di connessione al database &#8211; che rappresentano i due elementi fondamentali per la buona costruzione di un sito.</p>
<p>A un certo punto, però, vi accorgete che nel pc degli utenti il vostro sito risente in <strong>alcuni casi dei rallentamenti</strong>. Quello che per voi era un codice «perfetto» (una parola che non si dovrebbe mai usare nella programmazione) in realtà ha degli scogli che gli utenti si trovano a dover aggirare.</p>
<p>Provate a immaginare un ecommerce complesso: centinaia di prodotti nello store e migliaia di query processate ogni giorno; se il sito è visitato, le pagine aperte dall&#8217;utente possono essere decine. Con il tempo sempre più agli sgoccioli in quest&#8217;Era moderna, l&#8217;utente non ha la pazienza per aspettare che una pagina pesante si carichi del tutto o che la sua adsl (se la possiede) superi i blocchi di linea che ogni tanto si presentano in Italia.<br />
Nella peggiore delle ipotesi, potrebbe decidere di abbandonare il sito e cercare un altro negozio online.</p>
<p>C&#8217;è altro che possiamo fare per velocizzare l&#8217;apertura delle pagine del nostro sito?<br />
La risposta, naturalmente, è affermativa: <strong>c&#8217;è sempre qualcosa da imparare e da migliorare</strong>.<br />
In questo articolo vi propongo alcuni spunti di sbocco e alcune accortezze da seguire per ridurre al minimo i tempi di caricamento della pagina lato utente.</p>
<p>Teniamo presente, comunque, che esistono online ottimi strumenti per monitorare la velocità delle nostre pagine e capire dove mettere le mani per migliorare. Uno di questi strumenti è il <a title="Google Page Speed - Tool per il monitoraggio della velocità di un sito" href="https://developers.google.com/speed/pagespeed/insights" target="_blank" rel="noopener">Page Speed di Google</a>: basta inserire l&#8217;url del sito e avrete una lista di &#8220;problemi&#8221; che stanno indigesti al più grande motore di ricerca e che pertanto dovrebbero essere risolti il prima possibile.</p>
<h3 style="margin-top: 50px; margin-bottom: 20px; color: #c00;">Accortezze generali</h3>
<p>Prima di entrare in &#8220;fase avanzata&#8221;, vediamo qualche <strong>linea di base da seguire nella costruzione di un qualsiasi sito</strong> internet. Se avete qualche anno di esperienza nel settore probabilmente avrete già evaso tutti o quasi i seguenti punti, ma una ripassata non fa mai male.<br />
Cito i punti senza addentrarmi nella questione, perché potete facilmente informarvi con una ricerca online.</p>
<p>&#8211; Usare i <strong>CSS Sprites</strong> per le immagini, ovvero combinare le immagini di sfondo in un&#8217;unica immagine e usare le direttive &#8220;background-image&#8221; e &#8220;background-position&#8221; per mostrare il segmento voluto.</p>
<p>&#8211; Usare un sistema di caching (vedi sotto).</p>
<p>&#8211; Usare la compressione delle pagine (vedi sotto).</p>
<p>&#8211; Mettere i fogli di stile in alto nella pagina: in questo modo le pagine si renderizzano progressivamente.</p>
<p>&#8211; Quando possibile, mettere i <strong>javascript in basso nella pagina</strong>.</p>
<p>&#8211; Quando possibile, evitare stili in linea e javascript nella pagina: meglio caricare css e javascript da file esterni.</p>
<p>&#8211; Quando possibile, <strong>evitare i redirect</strong> 301 e 302.</p>
<p>&#8211; Nelle chiamate Ajax al di sotto dei 2K di dati, usare GET anziché POST: con i POST, infatti, prima invia l&#8217;header e poi i dati; invece con i GET invia un unico pacchetto.</p>
<p>&#8211; Ridurre il più possibile le dimensioni dei cookies.</p>
<h3 style="margin-top: 50px; margin-bottom: 20px; color: #c00;">Htaccess e html &#8211; Abilitare il caching delle pagine</h3>
<p>Abilitando il <strong>sistema di headers caching</strong>, se un utente torna a visitare il nostro sito eviterà di scaricare di nuovo le risorse &#8211; con ovvio incremento di prestazioni e di velocità.<br />
Il sistema di caching andrebbe applicato a tutti gli elementi statici: immagini, js, css, pdf, media files, ecc.</p>
<p>La cosa migliore è settare il tempo di &#8220;Expires&#8221;, cioè il periodo dopo il quale le risorse vengono di nuovo scaricate, da un minimo di una settimana a un massimo di un anno (oltre un anno non è consigliabile).</p>
<p>Impostando il tempo nei metatag &#8220;Expires&#8221; oppure &#8220;Cache-Control: max-age&#8221;, <strong>il browser eviterà di controllare</strong> nel web server la disponibilità di nuove versioni dell&#8217;elemento.</p>
<p><span id="more-2003"></span></p>
<p>Con il seguente metatag si abiliterà il caching delle pagine:</p>
<div class="wp_syntax">
<table>
<tbody>
<tr>
<td class="line_numbers">
<pre>1</pre>
</td>
<td class="code">
<pre class="html4strict" style="font-family: monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">http-equiv</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"Cache-control"</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"public"</span>&gt;</span></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>Il valore &#8220;public&#8221; significa che le risorse saranno messe in una cache pubblica. All&#8217;occorrenza il valore può essere sostituito con &#8220;private&#8221; (=cache privata) oppure &#8220;no-cache&#8221; (=la pagina non viene messa in cache).</p>
<p><script type="text/javascript">// <![CDATA[
google_ad_client = "ca-pub-9984963620559913"; /* Sir Bit - post singolo rettangolare */ google_ad_slot = "5334366715"; google_ad_width = 336; google_ad_height = 280;
// ]]&gt;</script><br />
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">// <![CDATA[


// ]]&gt;</script></p>
<p>Entriamo più nello specifico.<br />
Copiate il seguente codice nell&#8217;htaccess: in questo modo si abiliterà l&#8217;<strong>expires per gli elementi &#8220;statici&#8221; della pagina</strong>. Nel seguente esempio, dopo 1 settimana (604800 secondi) gli elementi saranno scaricati di nuovo:</p>
<div class="wp_syntax">
<table>
<tbody>
<tr>
<td class="line_numbers">
<pre>1
2
3
4</pre>
</td>
<td class="code">
<pre class="text" style="font-family: monospace;"># caching 1 settimana (in secondi)
&lt;FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$"&gt;
Header set Cache-Control "max-age=604800, public"
&lt;/FilesMatch&gt;</pre>
</td>
</tr>
</tbody>
</table>
</div>
<h3 style="margin-top: 50px; margin-bottom: 20px; color: #c00;">Compressione delle pagine con php</h3>
<p><em>Nota</em>: i file già compressi non hanno bisogno di compressione, per cui se possibile evitare di comprimere file come mp3, zip, ecc.</p>
<p>Usando un <strong>sistema di compressione delle pagine</strong>, si può velocizzare (a volte di molto) il tempo di risposta HTTP. L&#8217;header &#8220;Accept-Econding&#8221; supporta la compressione tramite gzip e deflate.<br />
In pratica, grazie a questo header, il browser invia la richiesta &#8220;Accept-Encoding: gzip, deflate&#8221; e il server risponderà con &#8220;Contenet-Encoding: gzip&#8221;; se il server non invia questa risposta, allora qualcosa è andato storto.</p>
<p>Gzip è il più efficiente (può ridurre fino al 70% le dimensioni di risposta) rispetto a deflate.</p>
<p>Prima di tutto occorre verificare che il server che ospita il nostro sito supporti la libreria gzip. In PHP basta lanciare il comando <em>phpinfo()</em> e controllare se il modulo &#8220;mod_gzip&#8221; (la libreria &#8220;zlib&#8221;) è abilitato.<br />
A questo punto, in ogni nostra pagina e, prima di qualsiasi altro codice, dobbiamo <strong>far uso dell&#8217;<em>ob_start</em> e dell&#8217;<em>ob_gzhandler</em> </strong>in modo che richiedano la compressione:</p>
<div class="wp_syntax">
<table>
<tbody>
<tr>
<td class="line_numbers">
<pre>1</pre>
</td>
<td class="code">
<pre class="php" style="font-family: monospace;"><span style="color: #990000;">ob_start</span><span style="color: #009900;">(</span> <span style="color: #0000ff;">'ob_gzhandler'</span> <span style="color: #009900;">)</span><span style="color: #339933;">;</span></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>Un modo più sicuro è far controllare a php se sia possibile abilitare la compressione; in caso contrario lanciare un comune <em>ob_start</em>:</p>
<div class="wp_syntax">
<table>
<tbody>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5</pre>
</td>
<td class="code">
<pre class="php" style="font-family: monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">(</span><span style="color: #990000;">substr_count</span><span style="color: #009900;">(</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">[</span><span style="color: #0000ff;">'HTTP_ACCEPT_ENCODING'</span><span style="color: #009900;">]</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'gzip'</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span>
	<span style="color: #990000;">ob_start</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'ob_gzhandler'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span><span style="color: #b1b100;">else</span><span style="color: #009900;">{</span>
	<span style="color: #990000;">ob_start</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>Subito dopo aver effettuate le modifiche, un modo per testare se il sito ha preso correttamente la compressione è passare il link della pagina a un tool dedicato per questo tipo di monitoraggio. Uno degli strumenti lo mette a disposizione <a title="GIDZipTest - Tool per il monitoraggio della compressione delle pagine" href="http://www.gidnetwork.com/tools/gzip-test.php" target="_blank" rel="noopener">GIDZipTest</a>.</p>
<p>Se il codice è stato correttamente settato, la risposta mostrerà tra gli header il tipo di compressione (voce &#8220;content-encoding&#8221;).</p>
<h3 style="margin-top: 50px; margin-bottom: 20px; color: #c00;">Comprimere determinati tipi di file con htaccess</h3>
<p>In questo caso il server deve avere abilitato il modulo &#8220;mod_deflate&#8221; (controllate lanciando sempre <em>phpinfo()</em>).<br />
Inserire nell&#8217;htaccess i seguenti codici:</p>
<div class="wp_syntax">
<table>
<tbody>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15</pre>
</td>
<td class="code">
<pre class="text" style="font-family: monospace;"># per comprire i file di tipo text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

# per comprire i file con estensione .html:
&lt;files *.html&gt;
SetOutputFilter DEFLATE
&lt;/files&gt;</pre>
</td>
</tr>
</tbody>
</table>
</div>The post <a href="https://www.manuelmarangoni.it/sir-bit/2003/php-html-velocizzare-il-sito-accorgimenti-caching-e-compressione-delle-pagine/">Php, html, htaccess – Velocizzare il sito – Accorgimenti, caching e compressione delle pagine</a> first appeared on <a href="https://www.manuelmarangoni.it/sir-bit">Sir Bit</a>.]]></content:encoded>
					
					<wfw:commentRss>https://www.manuelmarangoni.it/sir-bit/2003/php-html-velocizzare-il-sito-accorgimenti-caching-e-compressione-delle-pagine/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>WordPress 3.0 &#8211; Post personalizzati (Custom Post Type) &#8211; Esempio completo</title>
		<link>https://www.manuelmarangoni.it/sir-bit/1993/wordpress-3-0-post-personalizzati-custom-post-type-esempio-completo/</link>
					<comments>https://www.manuelmarangoni.it/sir-bit/1993/wordpress-3-0-post-personalizzati-custom-post-type-esempio-completo/#comments</comments>
		
		<dc:creator><![CDATA[Manuel Marangoni]]></dc:creator>
		<pubDate>Sun, 16 Feb 2014 21:27:49 +0000</pubDate>
				<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[wordpress]]></category>
		<guid isPermaLink="false">http://www.manuelmarangoni.it/sir-bit/?p=1993</guid>

					<description><![CDATA[<p>Un esempio completo, con plugin e file scaricabili gratuitamente, per creare i Custom Post Type in Wordpress. Creeremo un'area per gestire articoli separati.</p>
The post <a href="https://www.manuelmarangoni.it/sir-bit/1993/wordpress-3-0-post-personalizzati-custom-post-type-esempio-completo/">WordPress 3.0 – Post personalizzati (Custom Post Type) – Esempio completo</a> first appeared on <a href="https://www.manuelmarangoni.it/sir-bit">Sir Bit</a>.]]></description>
										<content:encoded><![CDATA[<p>Una delle funzionalità più utili e potenti messe a disposizione da WordPress è la possibilità di personalizzare a piacere l&#8217;uso degli articoli e delle categorie.</p>
<p>Faccio una premessa.<br />
Gli articoli che siamo abituati a usare hanno in realtà <strong>un «tipo» associato: il tipo &#8220;post&#8221;</strong>. Allo stesso modo, le pagine hanno associato il tipo &#8220;page&#8221;. Di default troviamo poi altri tre tipi: &#8220;attachment&#8221;, &#8220;revision&#8221; e &#8220;nav_menu_item&#8221;.</p>
<p>WordPress ci dà la possibilità di creare dei nuovi tipi, in pratica delle aree separate in cui possiamo inserire articoli e categorie senza intaccare quelli principali.<br />
Se, per esempio, abbiamo un sito che si occupa di musica e a un certo punto decidiamo di inserire una sezione per i film, ci sarà utile <strong>creare una nuova area per i film</strong>, i cui articoli saranno tenuti ben distinti da quelli della musica.<br />
I tipi personalizzati vengono denominati tecnicamente <em>Custom Post Type</em> (CPT).</p>
<p>In questo articolo vedremo un <strong>esempio completo e minimale su come realizzare i CPT</strong>.<br />
Nello specifico, il nostro cliente immaginario ci ha commissionato la realizzazione di un&#8217;area per inserire i calendari delle partite di andata e ritorno di una squadra di pallavolo.<br />
Quello di cui abbiamo bisogno è di creare un&#8217;area personalizzata chiamata &#8220;Calendario&#8221;, che si occupa esattamente come un qualsiasi post ma che in lato sito sarà visualizzata in una sezione a parte.</p>
<p>I file completi sono <strong>scaricabili gratuitamente al seguente link</strong>. All&#8217;interno troverete un txt contenente la descrizione generale con i passi da seguire.</p>
<div style="float: left; width: 100%; margin-top: 60px; margin-bottom: 40px;">
<div style="float: left; width: 100px;"><img decoding="async" title="Wordpress 3.0 - Post personalizzati (Custom Post Type) - Esempio completo" alt="Wordpress 3.0 - Post personalizzati (Custom Post Type) - Esempio completo" src="https://www.manuelmarangoni.it/images/zip_logo-80x80.png" /></div>
<div style="float: left; width: 350px; padding-top: 30px; font-size: 14px;"><strong><a title="Wordpress 3.0 - Post personalizzati (Custom Post Type) - Esempio completo" href="https://www.manuelmarangoni.it/download_script.php?idfile=40&amp;link=https://www.manuelmarangoni.it/sir-bit/1993/wordpress-3-0-post-personalizzati-custom-post-type-esempio-completo" target="_blank">Scarica gli script di questo articolo in formato zip</a></strong></div>
</div>
<h3 style="margin-top: 50px; margin-bottom: 20px; color: #c00;">Il plugin &#8220;MM Custom Type Calendario&#8221; [mm_custom_types.php]</h3>
<p>Le basi del CPT le costruiremo <strong>attraverso un comodo plugin</strong>, che potrà quindi essere esportato facilmente in altri progetti.<br />
Il file &#8220;<strong>mm_custom_types.php</strong>&#8221; nello zip va inserito così com&#8217;è nella cartella dei plugin di WordPress. Il plugin va poi attivato come al solito nell&#8217;apposita area dell&#8217;admi.</p>
<p>Vediamo il contenuto del plugin.<br />
Ho cercato di introdurre quanti più parametri possibili nella funzione, così da averli sottomano. Ogni parametro è commentato. In realtà, i valori attributi a gran parte di questi parametri sono quelli di default e quindi potevano essere omessi.<br />
Per personalizzare i parametri vedete la documentazione ufficiale della funzione &#8220;register_post_type()&#8221; di WordPress (a fondo articolo trovate il link diretto).<br />
<span id="more-1993"></span></p>
<p>Per prima cosa <strong>creiamo la funzione <em>crea_calendari()</em>.</strong> Sarà questa a definire i parametri per comporre il nostro CPT.<br />
All&#8217;interno della funzione richiameremo <em>register_post_type()</em>, facendogli passare il riferimento (&#8220;calendario&#8221;, che in pratica sarà il nome univoco che identificherà il nostro CPT) e l&#8217;array contenente tutti i parametri definiti.</p>
<div class="wp_syntax">
<table>
<tbody>
<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</pre>
</td>
<td class="code">
<pre class="php" style="font-family: monospace;"><span style="color: #000000; font-weight: bold;">function</span> crea_calendari<span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span>

    <span style="color: #000088;">$labels</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">(</span>
        <span style="color: #0000ff;">'name'</span>               <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">(</span><span style="color: #0000ff;">'Calendari'</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//nome plurale dell'etichetta</span>
        <span style="color: #0000ff;">'singular_name'</span>      <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">(</span><span style="color: #0000ff;">'Calendario'</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//nome singolare dell'etichetta</span>
	<span style="color: #0000ff;">'menu_name'</span>	     <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">(</span><span style="color: #0000ff;">'Calendari Menu'</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//nome del menu</span>
	<span style="color: #0000ff;">'all_items'</span>          <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">(</span><span style="color: #0000ff;">'Elenco Calendari'</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//testo per mostrare tutti gli elementi</span>
        <span style="color: #0000ff;">'add_new'</span>            <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">(</span><span style="color: #0000ff;">'Aggiungi Calendario'</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//testo del pulsante di aggiunta nuovo elemento</span>
        <span style="color: #0000ff;">'add_new_item'</span>       <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">(</span><span style="color: #0000ff;">'Nuovo Calendario'</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//testo da mostrare quando siamo all'interno di un nuovo elemento</span>
        <span style="color: #0000ff;">'edit_item'</span>          <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">(</span><span style="color: #0000ff;">'Modifica Calendario'</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//testo di modifica dell'elemento</span>
        <span style="color: #0000ff;">'new_item'</span>           <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">(</span><span style="color: #0000ff;">'Nuovo Calendario'</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//testo del nuovo elemento</span>
        <span style="color: #0000ff;">'view_item'</span>          <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">(</span><span style="color: #0000ff;">'Visualizza Calendario'</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//testo di visualizzazione dell'elemento</span>
        <span style="color: #0000ff;">'search_items'</span>       <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">(</span><span style="color: #0000ff;">'Cerca Calendario'</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//testo per la ricerca degli elementi</span>
        <span style="color: #0000ff;">'not_found'</span>          <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">(</span><span style="color: #0000ff;">'Calendario non trovato'</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//testo quando non esistono elementi nell'elenco</span>
        <span style="color: #0000ff;">'not_found_in_trash'</span> <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">(</span><span style="color: #0000ff;">'Calendario non trovato nel cestino'</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//testo quando non esistono elementi nel cestino</span>
    <span style="color: #009900;">)</span><span style="color: #339933;">;</span>

    <span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">(</span>
                <span style="color: #0000ff;">'labels'</span>             <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$labels</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'description'</span>	     <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Raccolta dei Calendari'</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//breve descrizione del post type</span>
                <span style="color: #0000ff;">'public'</span>             <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//se =true, viene visto sia da admin che in front-end</span>
		<span style="color: #0000ff;">'exclude_from_search'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//se =true gli elementi sono esclusi dalla ricerca front-end</span>
		<span style="color: #0000ff;">'publicly_queryable'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//indica se possono essere eseguite query lato front-end</span>
		<span style="color: #0000ff;">'show_ui'</span>		<span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//se =false, in admin non sarà mostrare l'interfaccia per la gestione del post type</span>
		<span style="color: #0000ff;">'show_in_nav_menus'</span>	 <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//indica se il post type può essere inserito nel nav menu</span>
		<span style="color: #0000ff;">'show_in_admin_bar'</span>	 <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//se =false, il post type non viene mostrato nel menu superiore "Nuovo" dell'admin</span>

		<span style="color: #666666; font-style: italic;">//indica la posizione nei confronti degli altri elementi nel menu dell'admin.</span>
		<span style="color: #666666; font-style: italic;">// 5 = sotto a Posts; 10 = a Media; 15 = a Links; 20 = a Pages; 25 = a Comments; 60 = al primo separatore;</span>
		<span style="color: #666666; font-style: italic;">// 65 = a Plugins; 70 = a Users; 75 = a Tools; 80 = a Settings; 100 = al secondo separatore</span>
		<span style="color: #0000ff;">'menu_position'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span>

		<span style="color: #0000ff;">'hierarchical'</span>       <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//indica se il post type ha un genitore (in genere usato solo per le pagine)</span>

		<span style="color: #666666; font-style: italic;">//indica i valori del add_post_type_support() , ovvero quali elementi standard associare al post type</span>
                <span style="color: #0000ff;">'supports'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">(</span>
                                <span style="color: #0000ff;">'title'</span><span style="color: #339933;">,</span>
                                <span style="color: #0000ff;">'editor'</span><span style="color: #339933;">,</span>
                                <span style="color: #0000ff;">'thumbnail'</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">'excerpt'</span>
                                <span style="color: #009900;">)</span><span style="color: #339933;">,</span>

		<span style="color: #666666; font-style: italic;">//indica quali taxonomies associare al post type</span>
		<span style="color: #666666; font-style: italic;">//il seguente, per esempio, permette di inserire categorie a cui associare i post type</span>
		<span style="color: #666666; font-style: italic;">//in mancanza non sarà possibile creare categorie di post</span>
		<span style="color: #0000ff;">'taxonomies'</span>		 <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'category'</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>

		<span style="color: #0000ff;">'has_archive'</span>        <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//abilita la pagina di archivio per il post type</span>

		<span style="color: #666666; font-style: italic;">//permette di riscrivere alcuni parametri per il post type, che altrimenti sarebbero uguali a quelli standard</span>
		<span style="color: #666666; font-style: italic;">//</span>
		<span style="color: #0000ff;">'rewrite'</span>            <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">(</span>
					<span style="color: #0000ff;">'slug'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'calendari'</span> <span style="color: #666666; font-style: italic;">//il post type è raggiungibile all'indirizzo "mio_sito.it/calendari"</span>
					<span style="color: #009900;">)</span>
    <span style="color: #009900;">)</span><span style="color: #339933;">;</span>

	<span style="color: #666666; font-style: italic;">//registra l'elemento "calendario" come post type, con gli argomenti definiti sopra</span>
	register_post_type<span style="color: #009900;">(</span><span style="color: #0000ff;">'calendario'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>

<span style="color: #009900;">}</span></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>La nostra funzione sarà poi inizializzata con questa riga:</p>
<div class="wp_syntax">
<table>
<tbody>
<tr>
<td class="line_numbers">
<pre>1</pre>
</td>
<td class="code">
<pre class="php" style="font-family: monospace;">add_action<span style="color: #009900;">(</span><span style="color: #0000ff;">'init'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'crea_calendari'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>Una volta attivato il plugin, nel menu laterale dell&#8217;admin <strong>avremo una nuova area: &#8220;Calendari Menu&#8221;</strong>. Da qui sarà possibile visualizzare l&#8217;elenco dei calendari, aggiungere un nuovo calendario e associare a ognuno di essi una categoria da creare. Proprio come nei normali post.</p>
<p><script type="text/javascript">// <![CDATA[
google_ad_client = "ca-pub-9984963620559913"; /* Sir Bit - post singolo rettangolare */ google_ad_slot = "5334366715"; google_ad_width = 336; google_ad_height = 280;
// ]]&gt;</script><br />
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">// <![CDATA[


// ]]&gt;</script></p>
<h3 style="margin-top: 50px; margin-bottom: 20px; color: #c00;">Visualizzare la pagina nel front-end : creare il link nel menu</h3>
<p>A questo punto possiamo gestire tutto in lato amministrazione, ma in lato front-end non abbiamo ancora una struttura adatta. Infatti, dobbiamo manipolare il &#8220;single.php&#8221; e il &#8220;archive.php&#8221; perché visualizzino i nostri CPT e non i soliti post.</p>
<p>Innanzitutto, dobbiamo creare un link per permettere all&#8217;utente di raggiungere la nostra area CPT, che <strong>nel nostro caso sarà &#8220;mio_sito.it/calendari&#8221;</strong>.<br />
Nell&#8217;admin entrate in &#8220;Aspetto =&gt; Menu&#8221;. Si presume che abbiate un&#8217;idea di dove ci troviamo e di come si gestiscono i menu in WordPress (se non è così vi consiglio di provare a smanettare: è piuttosto intuitivo; quest&#8217;area permette infatti di creare a piacere un menu di navigazione da mostrare in lato front-end).<br />
Nello spazio &#8220;link&#8221; a sinistra, aggiungete al vosto menu l&#8217;url &#8220;mio_sito.it/calendari&#8221; e il nome &#8220;Calendari&#8221;. Ricordate di salvare le modifiche.</p>
<h3 style="margin-top: 50px; margin-bottom: 20px; color: #c00;">Single-calendario.php e Archive-calendario.php</h3>
<p>Passiamo adesso a creare i nuovi file.<br />
Quello che dobbiamo fare è creare una nuova versione di &#8220;single.php&#8221; e &#8220;archive.php&#8221;, aggiungendo al loro nome il suffisso &#8220;-calendario&#8221; (in pratica creeremo &#8220;single-calendario.php&#8221; e &#8220;archive-calendario.php&#8221;).</p>
<p>Nello zip troverete due file: <strong>&#8220;single-calendario.php&#8221; e &#8220;archive-calendario.php&#8221;</strong>. Non dovrete fare altro che copiarli nella cartella del vostro tema.<br />
Da notare il suffisso di entrambi i file, &#8220;-calendario&#8221;, che è lo stesso nome con cui abbiamo registrato il nostro CPT. In pratica, WordPress riconoscerà automaticamente che questi due file sono rispettivamente il single e l&#8217;archive da usare per il nostro CPT.</p>
<p>Come li ho creati?<br />
Semplicemente, ho preso esattamente il <strong>contenuto di &#8220;single.php&#8221; e l&#8217;ho copiato in &#8220;single-calendario.php&#8221;</strong> (<em>nota</em>: nel mio caso faccio riferimento al tema &#8220;twentyfourteen&#8221; messo in dotazione durante l&#8217;installazione di WordPress: in base al tema che voi usate, il contenuto del single potrebbe variare leggermente).<br />
Dopodiché, ho aggiunto queste righe subito prima del ciclo che stampa gli articoli:</p>
<div class="wp_syntax">
<table>
<tbody>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5
6</pre>
</td>
<td class="code">
<pre class="php" style="font-family: monospace;"><span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">(</span>      
	<span style="color: #0000ff;">'post_type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'calendario'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'post__in'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">(</span>get_the_ID<span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> 
	<span style="color: #0000ff;">'posts_per_page'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span>
<span style="color: #009900;">)</span><span style="color: #339933;">;</span>
query_posts<span style="color: #009900;">(</span><span style="color: #000088;">$args</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>In pratica sto dicendo a WordPress: «Prendi tutti gli articoli di tipo &#8220;calendario&#8221; e stampami solo quello con id del post attuale».<br />
Questo perché ci troviamo all&#8217;interno di un articolo e quello che ci serve è estrarre e stampare le informazioni di quell&#8217;articolo.</p>
<p>Allo stesso modo, ho <strong>creato &#8220;archive-calendario.php&#8221; copiandolo da &#8220;archive.php&#8221;</strong> e aggiungendogli questa query iniziale:</p>
<div class="wp_syntax">
<table>
<tbody>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5</pre>
</td>
<td class="code">
<pre class="php" style="font-family: monospace;"><span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">(</span>      
	<span style="color: #0000ff;">'post_type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'calendario'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'posts_per_page'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10</span>
<span style="color: #009900;">)</span><span style="color: #339933;">;</span>
query_posts<span style="color: #009900;">(</span><span style="color: #000088;">$args</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>In questo caso specifico a WordPress di prendere tutti gli articoli di tipo &#8220;calendario&#8221; e di stamparmi nella pagina solo i primi 10.</p>
<h3 style="margin-top: 50px; margin-bottom: 20px; color: #c00;">Note di chiarezza conclusive</h3>
<p>Come abbiamo capito, la particolarità di WordPress è che leggerà automaticamente la presenza di un nuovo &#8220;single-{nome_custom}.php&#8221; e &#8220;archive-{nome_custom}.php&#8221;, dove a {nome_custom} dobbiamo inserire il nome del nostro CPT.</p>
<p>Entrando più nello specifico, quando nel front-end cliccheremo <strong>sul link &#8220;mio_sito.php/calendari&#8221; finiremo nella pagina di &#8220;archive-calendario.php&#8221;</strong>, che conterrà l&#8217;elenco dei primi 10 articoli-calendario.<br />
Se cliccherete su uno degli articoli, verrete indirizzati alla pagina &#8220;single-calendario.php&#8221;, che mostrerà le informazioni del singolo articolo.</p>
<p>Naturalmente il contenuto di &#8220;single-calendario.php&#8221; e &#8220;archive-calendario.php&#8221; potrete personalizzarlo a piacere, a seconda delle vostre competenze nell&#8217;uso del codice php e di WordPress.</p>
<p>Per avere un&#8217;idea sul campo di cosa stiamo parlando, provate a creare alcuni articoli e vedete cosa accade quando visitere la pagina &#8220;mio_sito.php/calendari&#8221;.</p>
<div style="margin-top: 100px; margin-bottom: 20px;"><strong><em>Fonti esterne</em></strong></div>
<p><a title="I Post Type e i Custom Post Type sul codex ufficiale di WordPress" href="http://codex.wordpress.org/Post_Types" target="_blank">I Post Type e i Custom Post Type</a> sul codex ufficiale di WordPress</p>
<p><a title="La funzione register_post_type" href="http://codex.wordpress.org/Function_Reference/register_post_type" target="_blank">La funzione register_post_type</a> sul codex ufficiale di WordPress</p>The post <a href="https://www.manuelmarangoni.it/sir-bit/1993/wordpress-3-0-post-personalizzati-custom-post-type-esempio-completo/">WordPress 3.0 – Post personalizzati (Custom Post Type) – Esempio completo</a> first appeared on <a href="https://www.manuelmarangoni.it/sir-bit">Sir Bit</a>.]]></content:encoded>
					
					<wfw:commentRss>https://www.manuelmarangoni.it/sir-bit/1993/wordpress-3-0-post-personalizzati-custom-post-type-esempio-completo/feed/</wfw:commentRss>
			<slash:comments>16</slash:comments>
		
		
			</item>
		<item>
		<title>PHP &#8211; Censurare o sostituire una serie di parole con un&#8217;altra</title>
		<link>https://www.manuelmarangoni.it/sir-bit/1980/php-censura-o-sostituire-una-serie-di-parole-con-unaltra/</link>
					<comments>https://www.manuelmarangoni.it/sir-bit/1980/php-censura-o-sostituire-una-serie-di-parole-con-unaltra/#respond</comments>
		
		<dc:creator><![CDATA[Manuel Marangoni]]></dc:creator>
		<pubDate>Mon, 20 Jan 2014 16:07:26 +0000</pubDate>
				<category><![CDATA[Php e mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[script]]></category>
		<guid isPermaLink="false">http://www.manuelmarangoni.it/sir-bit/?p=1980</guid>

					<description><![CDATA[<p>Una funzione da scaricare completa di esempio per sostituire, all'interno di un testo, una serie di parole passate con un'altra parola, oppure per censurarle.</p>
The post <a href="https://www.manuelmarangoni.it/sir-bit/1980/php-censura-o-sostituire-una-serie-di-parole-con-unaltra/">PHP – Censurare o sostituire una serie di parole con un’altra</a> first appeared on <a href="https://www.manuelmarangoni.it/sir-bit">Sir Bit</a>.]]></description>
										<content:encoded><![CDATA[<p>Quando all&#8217;utente si lascia la possibilità di commentare articoli, di recensire prodotti o di scrivere le proprie opinioni, può essere utile far uso di una <strong>funzione che censuri una serie di parole</strong> ritenute &#8220;scomode&#8221; o &#8220;di spam&#8221;.</p>
<p>Con la funzione che andiamo a vedere, basta creare un array di parole: lo script si occuperà di rintracciarle nel testo e di <strong>sostituirle con un&#8217;altra parola data</strong>.<br />
Se la parola di sostituzione non è stata passata, le stringhe da &#8220;eliminare&#8221; saranno semplicemente censurate da una serie di cancelletti (&#8220;#&#8221;).</p>
<p>Lo script, completo di esempio, è liberamente scaricabile dal link qui sotto.<br />
Non necessita di modifiche, a meno che non la vogliate integrare con ulteriori controlli sui parametri (che per semplicità ho ridotto al minimo).<span id="more-1980"></span></p>
<div style="float: left; width: 100%; margin-top: 60px; margin-bottom: 40px;">
<div style="float: left; width: 100px;"><img decoding="async" title="PHP - Censura o sostituire una serie di parole con un'altra" alt="PHP - Censura o sostituire una serie di parole con un'altra" src="https://www.manuelmarangoni.it/images/zip_logo-80x80.png" /></div>
<div style="float: left; width: 350px; padding-top: 30px; font-size: 14px;"><strong><a title="PHP - Censura o sostituire una serie di parole con un'altra" href="https://www.manuelmarangoni.it/download_script.php?idfile=39&amp;link=https://www.manuelmarangoni.it/sir-bit/1980/php-censura-o-sostituire-una-serie-di-parole-con-unaltra" target="_blank">Scarica gli script di questo articolo in formato zip</a></strong></div>
</div>
<h3 style="margin-top: 50px; margin-bottom: 20px; color: #c00;">La funzione di sostituzione stringa</h3>
<p>La funzione permette il passaggio di due parametri e di un terzo opzionale.</p>
<p><em>$string</em> è il testo da analizzare, dove presumibilmente potrebbero trovarsi le parole da censurare.</p>
<p><em>$bad_words</em> è l&#8217; array contenente <strong>le stringhe da convertire</strong>.</p>
<p><em>$new_string</em> è la stringa con cui le parole da censurare saranno convertite. E&#8217; un parametro opzionale: se non passato, le stringhe di $bad_words saranno semplicemente <strong>oscurate con #</strong> (uno per carattere).</p>
<p><script type="text/javascript">// <![CDATA[
google_ad_client = "ca-pub-9984963620559913"; /* Sir Bit - post singolo rettangolare */ google_ad_slot = "5334366715"; google_ad_width = 336; google_ad_height = 280;
// ]]&gt;</script><br />
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">// <![CDATA[


// ]]&gt;</script></p>
<div class="wp_syntax">
<table>
<tbody>
<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</pre>
</td>
<td class="code">
<pre class="php" style="font-family: monospace;"><span style="color: #000000; font-weight: bold;">function</span> replace_words<span style="color: #009900;">(</span> <span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bad_words</span><span style="color: #339933;">,</span> <span style="color: #000088;">$new_string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span> <span style="color: #009900;">)</span><span style="color: #009900;">{</span>

	<span style="color: #666666; font-style: italic;">//controllo di sicurezza: $bad_words deve essere un array</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">(</span><span style="color: #339933;">!</span><span style="color: #990000;">is_array</span><span style="color: #009900;">(</span><span style="color: #000088;">$bad_words</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$string</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">}</span>

	<span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">' '</span><span style="color: #339933;">.</span><span style="color: #000088;">$string</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">;</span>

	<span style="color: #666666; font-style: italic;">//delimitatori per caratteri non Unicode</span>
	<span style="color: #000088;">$delimiter</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'[-_\'\"`(){}&lt;&gt;\[\]|!?@#%&amp;,.:;^~*+=\/ 0-9\n\r\t]'</span><span style="color: #339933;">;</span>

	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">(</span><span style="color: #000088;">$bad_words</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$value</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span>

		<span style="color: #666666; font-style: italic;">//stringhe sostituite da un'altra stringa</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">(</span><span style="color: #000088;">$new_string</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span>
			<span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">(</span><span style="color: #0000ff;">"/(<span style="color: #006699; font-weight: bold;">{$delimiter}</span>)("</span><span style="color: #339933;">.</span><span style="color: #990000;">str_replace</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'\*'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'\w*?'</span><span style="color: #339933;">,</span> <span style="color: #990000;">preg_quote</span><span style="color: #009900;">(</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'/'</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">.</span><span style="color: #0000ff;">")(<span style="color: #006699; font-weight: bold;">{$delimiter}</span>)/i"</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">"<span style="color: #000099; font-weight: bold;">\\</span>1<span style="color: #006699; font-weight: bold;">{$new_string}</span><span style="color: #000099; font-weight: bold;">\\</span>3"</span><span style="color: #339933;">,</span> <span style="color: #000088;">$string</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>

		<span style="color: #666666; font-style: italic;">//caratteri sostituiti con #	</span>
		<span style="color: #009900;">}</span><span style="color: #b1b100;">else</span><span style="color: #009900;">{</span>
			<span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">(</span><span style="color: #0000ff;">"/(<span style="color: #006699; font-weight: bold;">{$delimiter}</span>)("</span><span style="color: #339933;">.</span><span style="color: #990000;">str_replace</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'\*'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'\w*?'</span><span style="color: #339933;">,</span> <span style="color: #990000;">preg_quote</span><span style="color: #009900;">(</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'/'</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">.</span><span style="color: #0000ff;">")(<span style="color: #006699; font-weight: bold;">{$delimiter}</span>)/ie"</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">"'<span style="color: #000099; font-weight: bold;">\\</span>1'.str_repeat('#', strlen('<span style="color: #000099; font-weight: bold;">\\</span>2')).'<span style="color: #000099; font-weight: bold;">\\</span>3'"</span><span style="color: #339933;">,</span> <span style="color: #000088;">$string</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>

		<span style="color: #009900;">}</span>

	<span style="color: #009900;">}</span>

	<span style="color: #b1b100;">return</span> <span style="color: #990000;">trim</span><span style="color: #009900;">(</span><span style="color: #000088;">$string</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>

<span style="color: #009900;">}</span></pre>
</td>
</tr>
</tbody>
</table>
</div>
<h3 style="margin-top: 50px; margin-bottom: 20px; color: #c00;">Esempio d&#8217;uso</h3>
<p>Di seguito prendiamo come esempio un testo generico, un array di due parole da censurare e una breve stringa con cui sostituire le parole:</p>
<div class="wp_syntax">
<table>
<tbody>
<tr>
<td class="line_numbers">
<pre>1
2
3</pre>
</td>
<td class="code">
<pre class="php" style="font-family: monospace;"><span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">"Qualunque bug sufficientemente avanzato è indistinguibile da una caratteristica del software."</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$no_words</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'sufficientemente'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'indistinguibile'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">' [eliminata] '</span><span style="color: #339933;">;</span></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p><strong>Ogni occorrenza</strong> di «sufficientemente» e di «indistinguibile» sarà sostituira con « [eliminata] » (notare spazi laterali compresi).</p>
<p>I risultati saranno i seguenti:</p>
<div class="wp_syntax">
<table>
<tbody>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5</pre>
</td>
<td class="code">
<pre class="php" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">// " Qualunque bug [eliminata] avanzato è [eliminata] da una caratteristica del software. "</span>
<span style="color: #b1b100;">echo</span> replace_words<span style="color: #009900;">(</span><span style="color: #000088;">$text</span><span style="color: #339933;">,</span> <span style="color: #000088;">$no_words</span><span style="color: #339933;">,</span> <span style="color: #000088;">$string</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>

<span style="color: #666666; font-style: italic;">// " Qualunque bug ################ avanzato è ############### da una caratteristica del software. "</span>
<span style="color: #b1b100;">echo</span> replace_words<span style="color: #009900;">(</span><span style="color: #000088;">$text</span><span style="color: #339933;">,</span> <span style="color: #000088;">$no_words</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span></pre>
</td>
</tr>
</tbody>
</table>
</div>The post <a href="https://www.manuelmarangoni.it/sir-bit/1980/php-censura-o-sostituire-una-serie-di-parole-con-unaltra/">PHP – Censurare o sostituire una serie di parole con un’altra</a> first appeared on <a href="https://www.manuelmarangoni.it/sir-bit">Sir Bit</a>.]]></content:encoded>
					
					<wfw:commentRss>https://www.manuelmarangoni.it/sir-bit/1980/php-censura-o-sostituire-una-serie-di-parole-con-unaltra/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Php – Zippare una cartella con sottocartelle e file (creare un archivio)</title>
		<link>https://www.manuelmarangoni.it/sir-bit/1966/php-zippare-una-cartella-con-sottocartelle-e-file-creare-un-archivio/</link>
					<comments>https://www.manuelmarangoni.it/sir-bit/1966/php-zippare-una-cartella-con-sottocartelle-e-file-creare-un-archivio/#comments</comments>
		
		<dc:creator><![CDATA[Manuel Marangoni]]></dc:creator>
		<pubDate>Mon, 25 Nov 2013 05:30:29 +0000</pubDate>
				<category><![CDATA[Php e mysql]]></category>
		<category><![CDATA[archivi]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[script]]></category>
		<guid isPermaLink="false">http://www.manuelmarangoni.it/sir-bit/?p=1966</guid>

					<description><![CDATA[<p>Il php mette a disposizione una classe, chiamata ZipArchive, il cui scopo è di creare archivi di file e di cartelle. Uno script gratuito per creare uno zip.</p>
The post <a href="https://www.manuelmarangoni.it/sir-bit/1966/php-zippare-una-cartella-con-sottocartelle-e-file-creare-un-archivio/">Php – Zippare una cartella con sottocartelle e file (creare un archivio)</a> first appeared on <a href="https://www.manuelmarangoni.it/sir-bit">Sir Bit</a>.]]></description>
										<content:encoded><![CDATA[<p>Il php mette a disposizione una classe, chiamata ZipArchive, il cui scopo è di creare archivi di file e di cartelle. In soldoni, con questa classe possiamo <strong>creare e leggere file zip</strong>, che saranno o sono salvati sul server.<br />
L&#8217;utilità può essere molteplice, come per esempio il backup di alcune cartelle o la compressione di alcuni file particolarmente pesanti prima di metterli a disposizione del pubblico.</p>
<p>In questo articolo vedremo uno <strong>script per aggiungere una cartella a un archivio</strong>. Ogni sua sottocartella e relativi file saranno aggiunti all&#8217;archivio allo stesso modo.<br />
Se l&#8217;archivio non esiste già, sarà creato.</p>
<p>Lo script può essere usato così com&#8217;è, modificando esclusivamente le variabili:<br />
$dir : cartella di destinazione dell&#8217;archivio<br />
$name : nome dell&#8217;archivio da creare/leggere<br />
$dir_name : nome della cartella da aggiungere all&#8217;archivio</p>
<p>L&#8217;archivio zip sarà <strong>creato allo stesso livello della cartella</strong> da aggiungervi. Naturalmente, con piccole modifiche alle varibili è possibile dargli in pasto qualsiasi percorso.</p>
<div style="float: left; width: 100%; margin-top: 60px; margin-bottom: 40px;">
<div style="float: left; width: 100px;"><img decoding="async" title="Php – Zippare una cartella con sottocartelle e file (creare un archivio)" alt="Php – Zippare una cartella con sottocartelle e file (creare un archivio)" src="https://www.manuelmarangoni.it/images/zip_logo-80x80.png" /></div>
<div style="float: left; width: 350px; padding-top: 30px; font-size: 14px;"><strong><a title="Php – Zippare una cartella con sottocartelle e file (creare un archivio)" href="https://www.manuelmarangoni.it/download_script.php?idfile=38&amp;link= https://www.manuelmarangoni.it/sir-bit/1966/php-zippare-una-cartella-con-sottocartelle-e-file-creare-un-archivio" target="_blank">Scarica gli script di questo articolo in formato zip</a></strong></div>
</div>
<p><span id="more-1966"></span></p>
<p><script type="text/javascript">// <![CDATA[
google_ad_client = "ca-pub-9984963620559913"; /* Sir Bit - post singolo rettangolare */ google_ad_slot = "5334366715"; google_ad_width = 336; google_ad_height = 280;
// ]]&gt;</script><br />
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">// <![CDATA[


// ]]&gt;</script></p>
<div class="wp_syntax">
<table>
<tbody>
<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
65
66
67
68
69
70</pre>
</td>
<td class="code">
<pre class="php" style="font-family: monospace;"> 
<span style="color: #009933; font-style: italic;">/**
* Aggiunge una cartella e le sue sottocartelle e i file contenuti a un archivio
* se l'archivio non esiste, lo crea
*/</span>

<span style="color: #666666; font-style: italic;">//creazione dell'oggetto di classe</span>
<span style="color: #000088;">$zip</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ZipArchive<span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>

<span style="color: #666666; font-style: italic;">//cartella generale</span>
<span style="color: #000088;">$dir</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">"test/zip_example/"</span><span style="color: #339933;">;</span>

<span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">"test_with_subfolders.zip"</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//nome dell'archivio</span>
<span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dir</span><span style="color: #339933;">.</span><span style="color: #000088;">$name</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//indirizzo completo dell'archivio</span>

<span style="color: #000088;">$zip</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">open</span><span style="color: #009900;">(</span><span style="color: #000088;">$filename</span><span style="color: #339933;">,</span> ZipArchive<span style="color: #339933;">::</span><span style="color: #004000;">CREATE</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> 

<span style="color: #000088;">$dir_name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'folder_to_add'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//nome della cartella da zippare</span>
<span style="color: #000088;">$dir_path</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dir</span><span style="color: #339933;">.</span><span style="color: #000088;">$dir_name</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//indirizzo completo della cartella da zippare</span>

<span style="color: #b1b100;">if</span><span style="color: #009900;">(</span><span style="color: #339933;">!</span><span style="color: #990000;">is_dir</span><span style="color: #009900;">(</span><span style="color: #000088;">$dir_path</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span> 
    <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">(</span><span style="color: #0000ff;">'La cartella '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$dir</span><span style="color: #339933;">.</span><span style="color: #000088;">$dir_path</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' non esiste'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">}</span> 

<span style="color: #666666; font-style: italic;">//aggiunge lo slash finale (o corrispettivo) se non presente nel nome cartella</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">(</span><span style="color: #990000;">substr</span><span style="color: #009900;">(</span><span style="color: #000088;">$dir_path</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span> <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">DIRECTORY_SEPARATOR</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span> 
    <span style="color: #000088;">$dir_path</span><span style="color: #339933;">.=</span> <span style="color: #009900; font-weight: bold;">DIRECTORY_SEPARATOR</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">}</span> 

<span style="color: #000088;">$dirStack</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">(</span><span style="color: #000088;">$dir_path</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> 

<span style="color: #666666; font-style: italic;">//trova l'indice da cui inizia l'ultima cartella</span>
<span style="color: #000088;">$cutFrom</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strrpos</span><span style="color: #009900;">(</span><span style="color: #990000;">substr</span><span style="color: #009900;">(</span><span style="color: #000088;">$dir_path</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'/'</span><span style="color: #009900;">)</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>

<span style="color: #b1b100;">while</span><span style="color: #009900;">(</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">(</span><span style="color: #000088;">$dirStack</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span>

    <span style="color: #000088;">$currentDir</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_pop</span><span style="color: #009900;">(</span><span style="color: #000088;">$dirStack</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> 
    <span style="color: #000088;">$filesToAdd</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> 

    <span style="color: #000088;">$dir_folder</span> <span style="color: #339933;">=</span> <span style="color: #990000;">dir</span><span style="color: #009900;">(</span><span style="color: #000088;">$currentDir</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> 
    <span style="color: #b1b100;">while</span><span style="color: #009900;">(</span> <span style="color: #009900; font-weight: bold;">false</span> <span style="color: #339933;">!==</span> <span style="color: #009900;">(</span><span style="color: #000088;">$node</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dir_folder</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #009900;">)</span><span style="color: #009900;">{</span> 

        <span style="color: #b1b100;">if</span><span style="color: #009900;">(</span> <span style="color: #009900;">(</span><span style="color: #000088;">$node</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'..'</span><span style="color: #009900;">)</span> <span style="color: #339933;">||</span> <span style="color: #009900;">(</span><span style="color: #000088;">$node</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'.'</span><span style="color: #009900;">)</span> <span style="color: #009900;">)</span><span style="color: #009900;">{</span> 
            <span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span> 
        <span style="color: #009900;">}</span> 

        <span style="color: #b1b100;">if</span><span style="color: #009900;">(</span><span style="color: #990000;">is_dir</span><span style="color: #009900;">(</span><span style="color: #000088;">$currentDir</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$node</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span> 
            <span style="color: #990000;">array_push</span><span style="color: #009900;">(</span><span style="color: #000088;">$dirStack</span><span style="color: #339933;">,</span> <span style="color: #000088;">$currentDir</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$node</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> 
        <span style="color: #009900;">}</span> 

        <span style="color: #b1b100;">if</span><span style="color: #009900;">(</span><span style="color: #990000;">is_file</span><span style="color: #009900;">(</span><span style="color: #000088;">$currentDir</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$node</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span> 
            <span style="color: #000088;">$filesToAdd</span><span style="color: #009900;">[</span><span style="color: #009900;">]</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$node</span><span style="color: #339933;">;</span> 
        <span style="color: #009900;">}</span> 

    <span style="color: #009900;">}</span> 

    <span style="color: #000088;">$localDir</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">(</span><span style="color: #000088;">$currentDir</span><span style="color: #339933;">,</span> <span style="color: #000088;">$cutFrom</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> 
    <span style="color: #000088;">$zip</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addEmptyDir</span><span style="color: #009900;">(</span><span style="color: #000088;">$localDir</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> 

    <span style="color: #b1b100;">foreach</span><span style="color: #009900;">(</span><span style="color: #000088;">$filesToAdd</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$file</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span> 
        <span style="color: #000088;">$zip</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addFile</span><span style="color: #009900;">(</span><span style="color: #000088;">$currentDir</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #000088;">$localDir</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$file</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> 
    <span style="color: #009900;">}</span> 

<span style="color: #009900;">}</span> 

<span style="color: #000088;">$zip</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">close</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span></pre>
</td>
</tr>
</tbody>
</table>
</div>
<div style="margin-top: 100px; margin-bottom: 20px;"><strong><em>Fonti esterne</em></strong></div>
<p><a title="La classe ZipArchive su php.net" href="http://php.net/manual/en/class.ziparchive.php" target="_blank">La classe ZipArchive su php.net</a></p>The post <a href="https://www.manuelmarangoni.it/sir-bit/1966/php-zippare-una-cartella-con-sottocartelle-e-file-creare-un-archivio/">Php – Zippare una cartella con sottocartelle e file (creare un archivio)</a> first appeared on <a href="https://www.manuelmarangoni.it/sir-bit">Sir Bit</a>.]]></content:encoded>
					
					<wfw:commentRss>https://www.manuelmarangoni.it/sir-bit/1966/php-zippare-una-cartella-con-sottocartelle-e-file-creare-un-archivio/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
		<item>
		<title>PHP e XML &#8211; Leggere i feed rss da link online</title>
		<link>https://www.manuelmarangoni.it/sir-bit/1955/php-e-xml-leggere-i-feed-rss-da-link-online/</link>
					<comments>https://www.manuelmarangoni.it/sir-bit/1955/php-e-xml-leggere-i-feed-rss-da-link-online/#respond</comments>
		
		<dc:creator><![CDATA[Manuel Marangoni]]></dc:creator>
		<pubDate>Wed, 09 Oct 2013 15:25:34 +0000</pubDate>
				<category><![CDATA[Php e mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[xml]]></category>
		<guid isPermaLink="false">http://www.manuelmarangoni.it/sir-bit/?p=1955</guid>

					<description><![CDATA[<p>Uno script liberamente scaricabile per recuperare il contenuto dei feed rss di un sito, facendo uso delle funzioni native php per la lettura degli xml.</p>
The post <a href="https://www.manuelmarangoni.it/sir-bit/1955/php-e-xml-leggere-i-feed-rss-da-link-online/">PHP e XML – Leggere i feed rss da link online</a> first appeared on <a href="https://www.manuelmarangoni.it/sir-bit">Sir Bit</a>.]]></description>
										<content:encoded><![CDATA[<p>I feed rss sono un <strong>ottimo strumento di propagazione dell&#8217;informazione</strong>. Vengono usati nei blog, ma anche nei siti di base (grazie alla diffusione di cms come WordPress) e soprattutto dai siti aziendali. Le ditte che comprendono numerosi prodotti, infatti, lo mettono spesso a disposizione per essere letti dagli aggregatori oppure, al contrario, recuperano spesso le schede dei prodotti da vendere da un link rss esterno.</p>
<p>Se avete bisogno di leggere un <strong>feed rss dotato di una struttura xml</strong> abbastanza standard non avrete grandi difficoltà: bastano poche righe di codice.<br />
Nell&#8217;esempio che vedremo sotto prenderò in esame i feed rss di <a title="Feed rss di Onemind.it" href="https://www.manuelmarangoni.it/onemind/feed/" target="_blank">www.onemind.it</a>, che hanno una struttura ad albero molto semplice. Per ogni articolo presente, estrarremo e <strong>recupereremo link, titolo e descrizione</strong>, che potremo immagazzinare in apposite variabili php oppure utilizzare come più ci aggrada.</p>
<p>Naturalmente strutture più complesse richiedono qualche sforzo in più. In genere, comunque, troverete le informazioni di un prodotto internate in un unico nodo xml.<br />
Lo script è scaricabile dal link qui sotto e liberamente personalizzabile.</p>
<div style="float:left; width:100%; margin-top:60px; margin-bottom:40px;">
<div style="float:left; width:100px;"><img decoding="async" title="PHP e XML - Leggere i feed rss da link online" src="https://www.manuelmarangoni.it/images/zip_logo-80x80.png" alt="PHP e XML - Leggere i feed rss da link online" /></div>
<div style="float:left; width:350px; padding-top:30px; font-size:14px;"><strong><a title="PHP e XML - Leggere i feed rss da link online" href="https://www.manuelmarangoni.it/download_script.php?idfile=37&amp;link=https://www.manuelmarangoni.it/sir-bit/?p=1955" target="_blank">Scarica gli script di questo articolo in formato zip</a></strong></div>
</div>
<h3 style="margin-top: 50px; margin-bottom: 20px; color: #c00;">Il nodo xml</h3>
<p>La struttura base del feed rss sarà qualcosa simile al seguente esempio.<br />
Ogni articolo sarà <strong>compreso tra i tag (nodo xml) &#8220;item&#8221;</strong>. Al suo interno troveremo i tag da estrarre: title, link, description.<br />
Per comodità ho eliminato tutti gli altri sotto-nodi non necessari.</p>
<div class="wp_syntax">
<table>
<tbody>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5
6
7
8
9
10
11
12
13</pre>
</td>
<td class="code">
<pre class="html" style="font-family: monospace;">&lt;item&gt;
&lt;title&gt;
15 immagini sulla Shadow Art: l’arte illusoria con le ombre
&lt;/title&gt;
&lt;link&gt;

<blockquote class="wp-embedded-content" data-secret="lZH8h39TpF"><a href="https://www.manuelmarangoni.it/onemind/3790/15-immagini-sulla-shadow-art-larte-illusoria-con-le-ombre/">15 immagini sulla Shadow Art: l&#8217;arte illusoria con le ombre </a></blockquote><iframe title="&#8220;15 immagini sulla Shadow Art: l&#8217;arte illusoria con le ombre [gallery]&#8221; &#8212; One Mind" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.manuelmarangoni.it/onemind/3790/15-immagini-sulla-shadow-art-larte-illusoria-con-le-ombre/embed/#?secret=lZH8h39TpF" data-secret="lZH8h39TpF" width="600" height="338" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>

&lt;/link&gt;
&lt;description&gt;
&lt;![CDATA[
La shadow art mescola luce e oggetti comuni, come legni o fili di ferro, per creare opere realistiche fatte di ombra. Gli autori migliori e le loro immagini.
]]&gt;
&lt;/description&gt;
&lt;/item&gt;</pre>
</td>
</tr>
</tbody>
</table>
</div>
<p><span id="more-1955"></span></p>
<h3 style="margin-top: 50px; margin-bottom: 20px; color: #c00;">Lo script per il recupero del feed</h3>
<p>I commenti dovrebbe bastare per coprire i dubbi. Nella pratica, non facciamo altro che <strong>scorrere tutti i tag &#8220;item&#8221;</strong> e recuperare i suoi tag interni, immagazzinandolo in variabili. Da un punto di vista logico, potete identificare il nodo &#8220;item&#8221; come un array e i suoi tag interni come gli elementi da recuperare.</p>
<p><script type="text/javascript">// <![CDATA[
google_ad_client = "ca-pub-9984963620559913"; /* Sir Bit - post singolo rettangolare */ google_ad_slot = "5334366715"; google_ad_width = 336; google_ad_height = 280;
// ]]&gt;</script><br />
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">// <![CDATA[


// ]]&gt;</script></p>
<div class="wp_syntax">
<table>
<tbody>
<tr>
<td class="line_numbers">
<pre>0
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</pre>
</td>
<td class="code">
<pre class="php" style="font-family: monospace;">&lt;?php
//crea un nuovo oggetto XML DOM
$xmldom = new DOMDocument();

//carica il contenuto del feed presente al link indicato
$xmldom-&gt;load("https://www.manuelmarangoni.it/onemind/feed/");

//recupera il nodo rappresentato da &lt;item&gt;
$nodo = $xmldom-&gt;getElementsByTagName("item");

//scorre tutti i nodi &lt;item&gt; della pagina
for($i=0; $i&lt;=$nodo-&gt;length-1; $i++){

	// Estraggo il contenuto dei singoli tag del nodo &lt;item&gt;
	$titolo = $nodo-&gt;item($i)-&gt;getElementsByTagName("title")-&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue;
	$collegamento = $nodo-&gt;item($i)-&gt;getElementsByTagName("link")-&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue;
	$descrizione = $nodo-&gt;item($i)-&gt;getElementsByTagName("description")-&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue;

	// Stampo a video i risultati
	?&gt;
	&lt;p&gt;
		&lt;a href="<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$collegamento</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>"&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$titolo</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/a&gt;&lt;br /&gt;
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$descrizione</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;/p&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009900;">}</span></pre>
</td>
</tr>
</tbody>
</table>
</div>The post <a href="https://www.manuelmarangoni.it/sir-bit/1955/php-e-xml-leggere-i-feed-rss-da-link-online/">PHP e XML – Leggere i feed rss da link online</a> first appeared on <a href="https://www.manuelmarangoni.it/sir-bit">Sir Bit</a>.]]></content:encoded>
					
					<wfw:commentRss>https://www.manuelmarangoni.it/sir-bit/1955/php-e-xml-leggere-i-feed-rss-da-link-online/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Funzione per convertire i caratteri stranieri in forma normalizzata</title>
		<link>https://www.manuelmarangoni.it/sir-bit/1947/funzione-per-convertire-i-caratteri-stranieri-in-forma-normalizzata/</link>
					<comments>https://www.manuelmarangoni.it/sir-bit/1947/funzione-per-convertire-i-caratteri-stranieri-in-forma-normalizzata/#respond</comments>
		
		<dc:creator><![CDATA[Manuel Marangoni]]></dc:creator>
		<pubDate>Wed, 18 Sep 2013 14:37:19 +0000</pubDate>
				<category><![CDATA[Php e mysql]]></category>
		<category><![CDATA[php]]></category>
		<guid isPermaLink="false">http://www.manuelmarangoni.it/sir-bit/?p=1947</guid>

					<description><![CDATA[<p>Una funzione per convertire i caratteri stranieri di una stringa in forma normalizzata, cioè in caratteri più simili a quelli italiani.</p>
The post <a href="https://www.manuelmarangoni.it/sir-bit/1947/funzione-per-convertire-i-caratteri-stranieri-in-forma-normalizzata/">Funzione per convertire i caratteri stranieri in forma normalizzata</a> first appeared on <a href="https://www.manuelmarangoni.it/sir-bit">Sir Bit</a>.]]></description>
										<content:encoded><![CDATA[<p>Nel vostro sito possono capitare utenti da ogni angolo del mondo, soprattutto se gestite un e-commerce internazionale. In casi come questi, può capitare che l&#8217;utente invii <strong>caratteri tipici delle lingue straniere</strong>, che magari non vorreste visualizzare nel vostro sito.</p>
<p>La seguente funzione prende un testo in entrate e <strong>converte tutti i caratteri stranieri in una forma normalizzata</strong> (per dirla terra a terra, in una forma più simile all&#8217;italiano).<br />
Vi sarà utile anche se gestite un blog con commenti aperti, visto che ormai è usanza sostituire coppie di lettere con caratteri stranieri: pensate per esempio alla &#8220;ß&#8221; tedesca, che tra i più giovani (per qualche ragione non ben comprensibile, dato lo sforzo per crearla su una tastiera comune) va spesso a sostituire la doppia &#8220;s&#8221;.</p>
<p>La funzione non è complessa e i commenti dovrebbero essere sufficienti per chiarire i piccoli dubbi.<br />
Tenete presente che la funzione è stata creata per sostituire i caratteri stranieri, ma nulla vieta di <strong>aggiungere nell&#8217;array altri caratteri da sostituire</strong> automaticamente quando si passa il testo (il mio consiglio è sempre quello, però, di separare le funzioni in base al loro scopo).<br />
<span id="more-1947"></span></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
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
</pre>
</td>
<td class="code">
<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> foreign_text_normalize<span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><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: #000088;">$string</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000088;">$return</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$string</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//stringa da ritornare</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//array contenente i caratteri stranieri da convertire</span>
	<span style="color: #000088;">$foreign_characters</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'ä|æ|ǽ'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'ae'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'ö|œ'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'oe'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'ü'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'ue'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'Ä'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Ae'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'Ü'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Ue'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'Ö'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Oe'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'À|Á|Â|Ã|Ä|Å|Ǻ|Ā|Ă|Ą|Ǎ'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'A'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'à|á|â|ã|å|ǻ|ā|ă|ą|ǎ|ª'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'a'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'Ç|Ć|Ĉ|Ċ|Č'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'C'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'ç|ć|ĉ|ċ|č'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'c'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'Ð|Ď|Đ'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'D'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'ð|ď|đ'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'d'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'È|É|Ê|Ë|Ē|Ĕ|Ė|Ę|Ě'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'E'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'è|é|ê|ë|ē|ĕ|ė|ę|ě'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'e'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'Ĝ|Ğ|Ġ|Ģ'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'G'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'ĝ|ğ|ġ|ģ'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'g'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'Ĥ|Ħ'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'H'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'ĥ|ħ'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'h'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'Ì|Í|Î|Ï|Ĩ|Ī|Ĭ|Ǐ|Į|İ'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'I'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'ì|í|î|ï|ĩ|ī|ĭ|ǐ|į|ı'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'i'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'Ĵ'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'J'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'ĵ'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'j'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'Ķ'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'K'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'ķ'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'k'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'Ĺ|Ļ|Ľ|Ŀ|Ł'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'L'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'ĺ|ļ|ľ|ŀ|ł'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'l'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'Ñ|Ń|Ņ|Ň'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'N'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'ñ|ń|ņ|ň|ŉ'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'n'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'Ò|Ó|Ô|Õ|Ō|Ŏ|Ǒ|Ő|Ơ|Ø|Ǿ'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'O'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'ò|ó|ô|õ|ō|ŏ|ǒ|ő|ơ|ø|ǿ|º'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'o'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'Ŕ|Ŗ|Ř'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'R'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'ŕ|ŗ|ř'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'r'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'Ś|Ŝ|Ş|Š'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'S'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'ś|ŝ|ş|š|ſ'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'s'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'Ţ|Ť|Ŧ'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'T'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'ţ|ť|ŧ'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'t'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'Ù|Ú|Û|Ũ|Ū|Ŭ|Ů|Ű|Ų|Ư|Ǔ|Ǖ|Ǘ|Ǚ|Ǜ'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'U'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'ù|ú|û|ũ|ū|ŭ|ů|ű|ų|ư|ǔ|ǖ|ǘ|ǚ|ǜ'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'u'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'Ý|Ÿ|Ŷ'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Y'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'ý|ÿ|ŷ'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'y'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'Ŵ'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'W'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'ŵ'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'w'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'Ź|Ż|Ž'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Z'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'ź|ż|ž'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'z'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'Æ|Ǽ'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'AE'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'ß'</span><span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'ss'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'Ĳ'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'IJ'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'ĳ'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'ij'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'Œ'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'OE'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'ƒ'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'f'</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$foreign_characters</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//suddivido la chiave nei suoi elementi (che sono separati da |)</span>
		<span style="color: #000088;">$ar</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'|'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ar</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$value2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">//sostituisce ogni carattere straniero con il suo corrispondente normalizzato</span>
			<span style="color: #000088;">$return</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$return</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$return</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre>
</td>
</tr>
</table>
</div>
<p>Ecco un <strong>esempio di utilizzo</strong> della funzione:</p>
<p><script type="text/javascript">// <![CDATA[
google_ad_client = "ca-pub-9984963620559913"; /* Sir Bit - post singolo rettangolare */ google_ad_slot = "5334366715"; google_ad_width = 336; google_ad_height = 280;
// ]]&gt;</script><br />
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">// <![CDATA[
// ]]&gt;</script></p>
<div class="wp_syntax">
<table>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5
</pre>
</td>
<td class="code">
<pre class="php" style="font-family:monospace;"><span style="color: #000088;">$testo</span><span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;L'albero del viale non era poi così großo&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$risultato</span> <span style="color: #339933;">=</span> foreign_text_normalize<span style="color: #009900;">&#40;</span><span style="color: #000088;">$testo</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//darà come risultato:</span>
<span style="color: #666666; font-style: italic;">//L'albero del viale non era poi così grosso</span></pre>
</td>
</tr>
</table>
</div>The post <a href="https://www.manuelmarangoni.it/sir-bit/1947/funzione-per-convertire-i-caratteri-stranieri-in-forma-normalizzata/">Funzione per convertire i caratteri stranieri in forma normalizzata</a> first appeared on <a href="https://www.manuelmarangoni.it/sir-bit">Sir Bit</a>.]]></content:encoded>
					
					<wfw:commentRss>https://www.manuelmarangoni.it/sir-bit/1947/funzione-per-convertire-i-caratteri-stranieri-in-forma-normalizzata/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Come aggiungere al sito il Facebook Like Box</title>
		<link>https://www.manuelmarangoni.it/sir-bit/1935/come-aggiungere-al-sito-il-facebook-like-box/</link>
					<comments>https://www.manuelmarangoni.it/sir-bit/1935/come-aggiungere-al-sito-il-facebook-like-box/#comments</comments>
		
		<dc:creator><![CDATA[Manuel Marangoni]]></dc:creator>
		<pubDate>Tue, 06 Aug 2013 05:30:12 +0000</pubDate>
				<category><![CDATA[Programmazione generale]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[plugin]]></category>
		<guid isPermaLink="false">http://www.manuelmarangoni.it/sir-bit/?p=1935</guid>

					<description><![CDATA[<p>Attraverso il sito, la Like Box permette agli utenti di seguire la vostra fan page su Facebook. Il plugin è rapido e semplice da installare.</p>
The post <a href="https://www.manuelmarangoni.it/sir-bit/1935/come-aggiungere-al-sito-il-facebook-like-box/">Come aggiungere al sito il Facebook Like Box</a> first appeared on <a href="https://www.manuelmarangoni.it/sir-bit">Sir Bit</a>.]]></description>
										<content:encoded><![CDATA[<div id="attachment_1937" style="width: 175px" class="wp-caption aligncenter"><a class="lightbox" title="Anteprima di una Facebook Like Box. Attraverso il sito, permette agli utenti di seguire la vostra fan page su Facebook" href="https://www.manuelmarangoni.it/sir-bit/wp-content/uploads/2013/08/Facebook-Like-Box-anteprima.jpg" target="_blank"><img decoding="async" aria-describedby="caption-attachment-1937" class="size-medium wp-image-1937 " title="Facebook Like Box - anteprima" alt="Facebook Like Box - anteprima" src="https://www.manuelmarangoni.it/sir-bit/wp-content/uploads/2013/08/Facebook-Like-Box-anteprima-165x300.jpg" width="165" height="300" /></a><p id="caption-attachment-1937" class="wp-caption-text">Anteprima di una Facebook Like Box. Attraverso il sito, permette agli utenti di seguire la vostra fan page su Facebook</p></div>
<p>Al giorno d&#8217;oggi, Facebook è probabilmente il principale canale per promuovere gli articoli di un sito e certamente è il primo &#8220;flusso social&#8221; da tenere in considerazione. Ecco perché <strong>costruire una fan page su Facebook</strong> è diventato ormai essenziale.</p>
<p>Vi sarete accorti che molti dei siti dotati di una fan page implementano un plugin particolare: non si tratta del solito pulsante &#8220;I like&#8221;, ma di un gadget più complesso che permette di <strong>visualizzare gli ultimi post della fan page</strong> e gli utenti che seguono la vostra pagina.<br />
Inoltre, attraverso questo plugin i visitatori potranno cliccare sul &#8220;mi piace&#8221; direttamente nel sito, senza cercare la vostra fan page direttamente su Facebook.</p>
<p>Vediamo brevemente come fare. Il processo è semplice e non richiede grandi competenze.</p>
<h3 style="margin-top: 50px; margin-bottom: 20px; color: #c00;">Le opzioni della Facebook Like Box</h3>
<p>Prima di tutto raggiungete la <a title="Pagina ufficiale di Facebook Like Box" href="https://developers.facebook.com/docs/reference/plugins/like-box/" target="_blank">pagina degli sviluppatori</a> della Like Box. Vedrete immediatamente un&#8217;anteprima generale (la prima immagine di questo articolo).</p>
<div id="attachment_1938" style="width: 460px" class="wp-caption aligncenter"><a class="lightbox" title="Le opzioni disponibili per la creazione della Facebook Like Box" href="https://www.manuelmarangoni.it/sir-bit/wp-content/uploads/2013/08/Facebook-Like-Box-opzioni.jpg" target="_blank"><img fetchpriority="high" decoding="async" aria-describedby="caption-attachment-1938" class="size-medium wp-image-1938 " title="Facebook Like Box - opzioni" alt="Facebook Like Box - opzioni" src="https://www.manuelmarangoni.it/sir-bit/wp-content/uploads/2013/08/Facebook-Like-Box-opzioni-450x125.jpg" width="450" height="125" srcset="https://www.manuelmarangoni.it/sir-bit/wp-content/uploads/2013/08/Facebook-Like-Box-opzioni-450x125.jpg 450w, https://www.manuelmarangoni.it/sir-bit/wp-content/uploads/2013/08/Facebook-Like-Box-opzioni.jpg 750w" sizes="(max-width: 450px) 100vw, 450px" /></a><p id="caption-attachment-1938" class="wp-caption-text">Le opzioni disponibili per la creazione della Facebook Like Box</p></div>
<p>Le opzioni a disposizione sono poche e comprensibili. Inserite innanzitutto il <strong>link alla vostra pagina Facebook</strong> nell&#8217;input &#8220;Facebook Page URL&#8221;. Se non avete particolari esigenze, la larghezza e l&#8217;altezza potete lasciarle così come sono (per l&#8217;altezza, in default è pensata per far comparire dieci facce-profilo di utenti).</p>
<p>Decidete quindi se volete un tema chiaro (<em>light</em>) o scuro (<em>dark</em>), a seconda del layout del vostro sito.</p>
<p>Le <strong>ultime quattro opzioni</strong> identificano cosa mostrare:<br />
&#8211; <em>Show header</em> : se spuntata mostra l&#8217;intestazione del plugin.<br />
&#8211; <em>Show stream</em> : se spuntata mostra gli ultimi post pubblicati sulla vostra fan page.<br />
&#8211; <em>Show faces</em> : se spuntata mostra le foto profilo degli utenti iscritti alla fan page.<br />
&#8211; <em>Show border</em> : se spuntata mantiene un bordo attorno al plugin.<br />
<span id="more-1935"></span></p>
<h3 style="margin-top: 50px; margin-bottom: 20px; color: #c00;">Ottenere il codice</h3>
<p>Una volta scelte le opzioni, non vi resta che cliccare sul pulsante &#8220;Get Code&#8221;.<br />
Si aprirà una finestra che vi permette di scegliere tra <strong>quattro tipologie di importazione</strong>. Sceglietene una e copiate il codice nel vostro sito come richiesto:</p>
<p><script type="text/javascript">// <![CDATA[
google_ad_client = "ca-pub-9984963620559913"; /* Sir Bit - post singolo rettangolare */ google_ad_slot = "5334366715"; google_ad_width = 336; google_ad_height = 280;
// ]]&gt;</script><br />
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">// <![CDATA[


// ]]&gt;</script></p>
<p>&#8211; <em>HTML5</em> : <strong>è la mia scelta consigliata</strong>, visto che quasi tutti i siti moderni riescono a supportarlo. In questo caso dovete copiare la prima parte del codice presentato in un qualsiasi punto all&#8217;interno del corpo della pagina. La seconda parte, invece, è il codice che effettivamente stamperà il plugin a video, per cui inseritelo dove volete che appaia.</p>
<p>&#8211; <em>XFBML</em> : la sigla sta per «eXtended Facebook Markup Language». Come dice il nome, è un linguaggio avanzato, sicuramente migliore dell&#8217;alternativa con iframe, visto che permette funzionalità avanzate. Tra le altre cose, attivando l&#8217;XFBML avrete l&#8217;opportunità di utilizzare l&#8217;Open Graph. Per l&#8217;attivazione e una spiegazione approfondita dell&#8217;XFBML vi rimando all&#8217;articolo di <a title="Abilitare XFBML sul vostro blog - Salvatore Capolupo" href="http://salvatorecapolupo.it/2010/10/abilitare-xfbml-sul-vostro-blog.html" target="_blank">Salvatore Capolupo</a>.</p>
<p>&#8211; <em>Iframe</em> e <em>URL</em>: l&#8217;iframe è un&#8217;alternativa che sconsiglio per i motivi visti sopra. In ogni caso questa opzione vi permette di implementare il plugin senza nessuno sforzo, se non il copiare il codice dove voluto all&#8217;interno della pagina web.</p>The post <a href="https://www.manuelmarangoni.it/sir-bit/1935/come-aggiungere-al-sito-il-facebook-like-box/">Come aggiungere al sito il Facebook Like Box</a> first appeared on <a href="https://www.manuelmarangoni.it/sir-bit">Sir Bit</a>.]]></content:encoded>
					
					<wfw:commentRss>https://www.manuelmarangoni.it/sir-bit/1935/come-aggiungere-al-sito-il-facebook-like-box/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>PHP &#8211; Funzione per convertire i byte in grandezze varie</title>
		<link>https://www.manuelmarangoni.it/sir-bit/1926/php-funzione-per-convertire-i-byte-in-grandezze-varie/</link>
					<comments>https://www.manuelmarangoni.it/sir-bit/1926/php-funzione-per-convertire-i-byte-in-grandezze-varie/#respond</comments>
		
		<dc:creator><![CDATA[Manuel Marangoni]]></dc:creator>
		<pubDate>Tue, 23 Jul 2013 05:30:10 +0000</pubDate>
				<category><![CDATA[Php e mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[script]]></category>
		<guid isPermaLink="false">http://www.manuelmarangoni.it/sir-bit/?p=1926</guid>

					<description><![CDATA[<p>Una funzione, liberamente scaricabile, per convertire i byte in grandezze superiori. Sarà affiancato il suffisso di grandezza opportuno.</p>
The post <a href="https://www.manuelmarangoni.it/sir-bit/1926/php-funzione-per-convertire-i-byte-in-grandezze-varie/">PHP – Funzione per convertire i byte in grandezze varie</a> first appeared on <a href="https://www.manuelmarangoni.it/sir-bit">Sir Bit</a>.]]></description>
										<content:encoded><![CDATA[<p>Il codice che segue &#8211; e che potete scaricare liberamente dal link sottostante per una più chiara comprensione &#8211; mostra una funzione che ha lo scopo di <strong>prendere un numero di byte e di trasformarlo</strong> in una grandezza appropriata.</p>
<p>Il procedimento è molto semplice e si basa sulla grandezza di $number, che viene confrontata prima con i valori più alti e, scalando, con valori sempre più bassi.</p>
<p>Per fare <strong>un esempio pratico</strong>, poniamo che a $number attribuiamo un valore di 20.971.520 byte (che corrisponde esattamente a 20 megabyte).<br />
I primi due <em>if</em> nella funzione risulteranno falsi, perché $number è inferiore a 1.000.000.000 . Pertanto entreremo nella condizione successiva, quella relativa ai megabyte.<br />
$number sarà convertito in megabyte e assumerà il nuovo valore di &#8220;20&#8221;.<br />
La funzione quindi stamperà &#8220;20 MB&#8221;.</p>
<p>Il secondo argomento della funzione, $precision, serve per <strong>specificare il grando di precisione del numero</strong> risultante, cioè quante cifre dopo la virgola dobbiamo prendere.</p>
<div style="float:left; width:100%; margin-top:60px; margin-bottom:40px;">
<div style="float:left; width:100px;"><img decoding="async" title="PHP - Funzione per convertire i byte in grandezze varie" src="https://www.manuelmarangoni.it/images/zip_logo-80x80.png" alt="PHP - Funzione per convertire i byte in grandezze varie" /></div>
<div style="float:left; width:350px; padding-top:30px; font-size:14px;"><strong><a title="PHP - Funzione per convertire i byte in grandezze varie" href="https://www.manuelmarangoni.it/download_script.php?idfile=36&amp;link=https://www.manuelmarangoni.it/sir-bit/1926/php-funzione-per-convertire-i-byte-in-grandezze-varie" target="_blank">Scarica gli script di questo articolo in formato zip</a></strong></div>
</div>
<p><span id="more-1926"></span></p>
<div class="wp_syntax">
<table>
<tbody>
<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</pre>
</td>
<td class="code">
<pre class="php" style="font-family: monospace;"><span style="color: #000000; font-weight: bold;">function</span> byte_convert<span style="color: #009900;">(</span><span style="color: #000088;">$number</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$precision</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span>

	<span style="color: #666666; font-style: italic;">//il numero passato deve essere intero</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">(</span> <span style="color: #339933;">!</span><span style="color: #990000;">is_numeric</span><span style="color: #009900;">(</span><span style="color: #000088;">$number</span><span style="color: #009900;">)</span> <span style="color: #009900;">)</span><span style="color: #009900;">{</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;">}</span>

	<span style="color: #000088;">$unit</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//suffisso dell'unità di misura</span>

	<span style="color: #666666; font-style: italic;">//terabyte</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">(</span><span style="color: #000088;">$number</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">1000000000000</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span>

		<span style="color: #000088;">$number</span> <span style="color: #339933;">=</span> <span style="color: #990000;">round</span><span style="color: #009900;">(</span><span style="color: #000088;">$number</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">1099511627776</span><span style="color: #339933;">,</span> <span style="color: #000088;">$precision</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$unit</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'TB'</span><span style="color: #339933;">;</span>

	<span style="color: #666666; font-style: italic;">//gigabyte</span>
	<span style="color: #009900;">}</span><span style="color: #b1b100;">elseif</span><span style="color: #009900;">(</span><span style="color: #000088;">$number</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">1000000000</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span>

		<span style="color: #000088;">$number</span> <span style="color: #339933;">=</span> <span style="color: #990000;">round</span><span style="color: #009900;">(</span><span style="color: #000088;">$number</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">1073741824</span><span style="color: #339933;">,</span> <span style="color: #000088;">$precision</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$unit</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'GB'</span><span style="color: #339933;">;</span>

	<span style="color: #666666; font-style: italic;">//megabyte</span>
	<span style="color: #009900;">}</span><span style="color: #b1b100;">elseif</span><span style="color: #009900;">(</span><span style="color: #000088;">$number</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">1000000</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span>

		<span style="color: #000088;">$number</span> <span style="color: #339933;">=</span> <span style="color: #990000;">round</span><span style="color: #009900;">(</span><span style="color: #000088;">$number</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">1048576</span><span style="color: #339933;">,</span> <span style="color: #000088;">$precision</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$unit</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'MB'</span><span style="color: #339933;">;</span>

	<span style="color: #666666; font-style: italic;">//kilobyte</span>
	<span style="color: #009900;">}</span><span style="color: #b1b100;">elseif</span><span style="color: #009900;">(</span><span style="color: #000088;">$number</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">1000</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span>

		<span style="color: #000088;">$number</span> <span style="color: #339933;">=</span> <span style="color: #990000;">round</span><span style="color: #009900;">(</span><span style="color: #000088;">$number</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">1024</span><span style="color: #339933;">,</span> <span style="color: #000088;">$precision</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$unit</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'KB'</span><span style="color: #339933;">;</span>

	<span style="color: #666666; font-style: italic;">//byte</span>
	<span style="color: #009900;">}</span><span style="color: #b1b100;">else</span><span style="color: #009900;">{</span>

		<span style="color: #000088;">$unit</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'byte'</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #990000;">number_format</span><span style="color: #009900;">(</span><span style="color: #000088;">$number</span><span style="color: #009900;">)</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">.</span><span style="color: #000088;">$unit</span><span style="color: #339933;">;</span>

	<span style="color: #009900;">}</span>

	<span style="color: #b1b100;">return</span> <span style="color: #990000;">number_format</span><span style="color: #009900;">(</span><span style="color: #000088;">$number</span><span style="color: #339933;">,</span> <span style="color: #000088;">$precision</span><span style="color: #009900;">)</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">.</span><span style="color: #000088;">$unit</span><span style="color: #339933;">;</span>

<span style="color: #009900;">}</span></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p><script type="text/javascript">// <![CDATA[
google_ad_client = "ca-pub-9984963620559913"; /* Sir Bit - post singolo rettangolare */ google_ad_slot = "5334366715"; google_ad_width = 336; google_ad_height = 280;
// ]]&gt;</script><br />
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">// <![CDATA[

// ]]&gt;</script></p>
<p>Vediamo <strong>un paio di esempi pratici</strong>, che trovate nello script da scaricare:</p>
<div class="wp_syntax">
<table>
<tbody>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5
6
7</pre>
</td>
<td class="code">
<pre class="php" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">//stampa: 48.7 MB</span>
<span style="color: #000088;">$byte</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">51107725</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> byte_convert<span style="color: #009900;">(</span><span style="color: #000088;">$byte</span><span style="color: #009900;">)</span><span style="color: #339933;">.</span><span style="color: #0000ff;">"&lt;br /&gt;&lt;br /&gt;"</span><span style="color: #339933;">;</span>

<span style="color: #666666; font-style: italic;">//stampa: 1.2 KB</span>
<span style="color: #000088;">$byte</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1179</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> byte_convert<span style="color: #009900;">(</span><span style="color: #000088;">$byte</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>La funzione può essere <strong>facilmente personalizzata a piacere</strong>. Con un po&#8217; di adattamento, per esempio, è possibile stampare a video tutti i formati possibile in base ai byte passati come argomento.</p>The post <a href="https://www.manuelmarangoni.it/sir-bit/1926/php-funzione-per-convertire-i-byte-in-grandezze-varie/">PHP – Funzione per convertire i byte in grandezze varie</a> first appeared on <a href="https://www.manuelmarangoni.it/sir-bit">Sir Bit</a>.]]></content:encoded>
					
					<wfw:commentRss>https://www.manuelmarangoni.it/sir-bit/1926/php-funzione-per-convertire-i-byte-in-grandezze-varie/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>PHP, MYSQL – Come connettersi a due database contemporaneamente</title>
		<link>https://www.manuelmarangoni.it/sir-bit/1917/php-mysql-come-connettersi-a-due-database-contemporaneamente/</link>
					<comments>https://www.manuelmarangoni.it/sir-bit/1917/php-mysql-come-connettersi-a-due-database-contemporaneamente/#respond</comments>
		
		<dc:creator><![CDATA[Manuel Marangoni]]></dc:creator>
		<pubDate>Thu, 11 Jul 2013 05:30:29 +0000</pubDate>
				<category><![CDATA[Php e mysql]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<guid isPermaLink="false">http://www.manuelmarangoni.it/sir-bit/?p=1917</guid>

					<description><![CDATA[<p>Vediamo due modi per connettersi contemporaneamente a due o più database, per gestire nello stesso sito i dati salvati su due server diversi.</p>
The post <a href="https://www.manuelmarangoni.it/sir-bit/1917/php-mysql-come-connettersi-a-due-database-contemporaneamente/">PHP, MYSQL – Come connettersi a due database contemporaneamente</a> first appeared on <a href="https://www.manuelmarangoni.it/sir-bit">Sir Bit</a>.]]></description>
										<content:encoded><![CDATA[<p>Se vi capita di gestire un cliente che dispone di due siti internet, è probabile che prima o poi vi ritroverete nella condizione di dover <strong>connettervi contemporaneamente a due database</strong>.<br />
Il motivo può essere vario: promuovere i prodotti di un dominio sull&#8217;altro dominio tramite una procedura automatica; recuperare informazioni o riferimenti dell&#8217;altro dominio; ecc.</p>
<p>La connessione tra due database non è sempre immediata.<br />
Prima di tutto dobbiamo distinguere due situazioni:<br />
1) i due database si trovano <strong>sullo stesso server</strong>: in questo caso host, user e password di connessione potrebbero coincidere<br />
2) i due database si trovano <strong>su server diversi</strong>: è necessario eseguire per forza due connessioni diverse</p>
<p>Qua sotto vi mostrerò due sistemi per connettervi a database multipli.<br />
In entrambe le situazioni:<br />
&#8211; i due database avranno il nome (poco fantasioso ma immediato) di <em>primo_database</em> e <em>secondo_database</em><br />
&#8211; in entrambi i database è presente una tabella chiamata <em>prodotti</em>, al cui interno ha un campo chiamato <em>id</em> (che rappresenta il campo autoincrementale dei record) e un campo chiamato <em>nome</em> (varchar di lunghezza a piacere)<br />
&#8211; poniamo per semplicità che in entrambe le tabelle &#8220;prodotti&#8221; ci siano dei record con id=2 . L&#8217;unica differenza sta nel campo <em>nome</em>, che avrà i seguenti valori:<br />
	&#8211; tabella di &#8220;primo_database&#8221;: nome=&#8221;libro&#8221;<br />
	&#8211; tabella di &#8220;secondo_database&#8221;: nome=&#8221;film&#8221;</p>
<p>In questo modo, quando stamperemo a video il campo &#8220;nome&#8221; del record con id=2 avremo la possibilità di capire se <strong>proviene dal primo database o dal secondo</strong>.</p>
<h3 style="margin-top:50px; margin-bottom:20px; color:#c00;">Connessione a due database sullo stesso server</h3>
<p>Se i due database si trovano sullo stesso server (probabilmente un server dedicato), in gran parte dei casi la connessione a entrambi avverrà con lo stesso host, user e password.<br />
Per distingue quale database coinvolge una delle nostre query, ci basta far <strong>precedere il nome della tabella <em>prodotti</em> dal nome del database</strong>.<br />
Quindi, se la tabella di ricerca della select sarà &#8220;primo_database.prodotti&#8221; significa che coinvolgerà la tabella &#8220;prodotti&#8221; di &#8220;primo_database&#8221;.<br />
Viceversa, se la tabella di ricerca della select sarà &#8220;secondo_database.prodotti&#8221; significa che coinvolgerà la tabella &#8220;prodotti&#8221; di &#8220;secondo_database&#8221;.<span id="more-1917"></span></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
23
24
25
26
27
28
29
30
31
32
</pre>
</td>
<td class="code">
<pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
* Connessione a due database sullo stesso server 
*/</span>
&nbsp;
<span style="color: #000088;">$db_host</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$db_user</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;root&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$db_password</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$db_name1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;primo_database&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$db_name2</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;secondo_database&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$db1</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db_host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db_user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db_password</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$db2</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db_host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db_user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db_password</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DB1'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DB2'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DBNAME1'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db_name1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DBNAME2'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db_name2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #000088;">$q</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM &quot;</span><span style="color: #339933;">.</span>DBNAME1<span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;.prodotti WHERE id=2&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$q</span><span style="color: #339933;">,</span> DB1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'nome'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br /&gt;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//stamperà &quot;libro&quot;</span>
&nbsp;
<span style="color: #000088;">$q</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM &quot;</span><span style="color: #339933;">.</span>DBNAME2<span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;.prodotti WHERE id=2&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$q</span><span style="color: #339933;">,</span> DB2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'nome'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//stamperà &quot;film&quot;</span></pre>
</td>
</tr>
</table>
</div>
<h3 style="margin-top:50px; margin-bottom:20px; color:#c00;">Connessione a due database (su stesso server o su server diversi)</h3>
<p>Un po&#8217; più macchinoso è il sistema da usare qualora i database risiedano su due server diversi.<br />
In questo caso host, user e password saranno diversi e <strong>le connessioni saranno completamente distinte</strong>.</p>
<p><script type="text/javascript">// <![CDATA[
google_ad_client = "ca-pub-9984963620559913"; /* Sir Bit - post singolo rettangolare */ google_ad_slot = "5334366715"; google_ad_width = 336; google_ad_height = 280;
// ]]&gt;</script><br />
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">// <![CDATA[
// ]]&gt;</script></p>
<p>Il modo migliore per gestire la situazione è di creare una funzione ad hoc, che ho chiamato <em>dbconn</em>, in cui come argomento passiamo semplice il valore &#8220;1&#8221; o &#8220;2&#8221; per far capire che vogliamo connetterci rispettivamente a &#8220;primo_database&#8221; e a &#8220;secondo_database&#8221;.<br />
Per connettersi al secondo database, quindi, dovremo semplicemente richiamare la funzione:</p>
<p>dbconn(2);</p>
<p>Una volta effettuata la connessione in questo modo, ci basterà eseguire normalmente la query sulla tabella &#8220;prodotti&#8221;.</p>
<p>L&#8217;aspetto macchinoso? Un simile sistema <strong>tiene aperta una sola connessione</strong> alla volta, per cui a ogni cambio di database dovremo richiamare la funzione &#8220;dbconn&#8221;.</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
23
24
25
26
27
28
29
30
31
32
33
34
</pre>
</td>
<td class="code">
<pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
* Connessione a due database
* utilizzabile sia per database nello stesso server che in server diversi
*/</span>
<span style="color: #000000; font-weight: bold;">function</span> dbconn<span style="color: #009900;">&#40;</span><span style="color: #000088;">$type</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$type</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #339933;">:</span>
			<span style="color: #000088;">$db1</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;host1&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;root&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;password1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;primo_database&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;2&quot;</span><span style="color: #339933;">:</span>
			<span style="color: #000088;">$db2</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;host2&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;root&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;password2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;second_database&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
dbconn<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$q</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM prodotti WHERE id=2&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$q</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'nome'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br /&gt;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//stamperà &quot;libro&quot;</span>
&nbsp;
dbconn<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$q</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM prodotti WHERE id=2&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$q</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'nome'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//stamperà &quot;film&quot;</span></pre>
</td>
</tr>
</table>
</div>The post <a href="https://www.manuelmarangoni.it/sir-bit/1917/php-mysql-come-connettersi-a-due-database-contemporaneamente/">PHP, MYSQL – Come connettersi a due database contemporaneamente</a> first appeared on <a href="https://www.manuelmarangoni.it/sir-bit">Sir Bit</a>.]]></content:encoded>
					
					<wfw:commentRss>https://www.manuelmarangoni.it/sir-bit/1917/php-mysql-come-connettersi-a-due-database-contemporaneamente/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>WordPress &#8211; Rendere sicuro il sito dagli attacchi di hacker</title>
		<link>https://www.manuelmarangoni.it/sir-bit/1910/wordpress-rendere-sicuro-il-sito-dagli-attacchi-di-hacker/</link>
					<comments>https://www.manuelmarangoni.it/sir-bit/1910/wordpress-rendere-sicuro-il-sito-dagli-attacchi-di-hacker/#respond</comments>
		
		<dc:creator><![CDATA[Manuel Marangoni]]></dc:creator>
		<pubDate>Thu, 27 Jun 2013 05:30:06 +0000</pubDate>
				<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[cms]]></category>
		<category><![CDATA[htaccess]]></category>
		<category><![CDATA[sicurezza]]></category>
		<category><![CDATA[wordpress]]></category>
		<guid isPermaLink="false">http://www.manuelmarangoni.it/sir-bit/?p=1910</guid>

					<description><![CDATA[<p>Alcune tecniche essenziali per rendere Wordpress più sicuro ed evitare l'intrusione da parte di hacker e cracker. Dall'htaccess ai protocolli https.</p>
The post <a href="https://www.manuelmarangoni.it/sir-bit/1910/wordpress-rendere-sicuro-il-sito-dagli-attacchi-di-hacker/">WordPress – Rendere sicuro il sito dagli attacchi di hacker</a> first appeared on <a href="https://www.manuelmarangoni.it/sir-bit">Sir Bit</a>.]]></description>
										<content:encoded><![CDATA[<p>WordPress è un cms che ha raggiunto una diffusione enorme negli ultimi anni, al punto che è usato da diversi utenti, neofiti o meno, per creare siti sia di livello elementare che di carattere professionale. Esistono temi, plugin e widget gratuiti sparsi per la rete.</p>
<p>La sua gratuità e il suo codice libero sono un’ottima cosa, perché permettono agli esperti di personalizzare le pagine discostandosi dalle caratteristiche base e di offrire agli altri plugin a costo zero. Tuttavia, proprio <strong>il suo essere libero è anche motivo di preoccupazione</strong> in alcuni casi.<br />
Un eventuale cracker (la versione “cattiva” degli hacker) può studiarsi il codice di un tema che magari è stato mal costruito e trovare un buco nel quale entrare. Oppure può “allungarvi” un plugin da lui costruito per sottrarvi dati preziosi o per arrivare persino al database.</p>
<p>Una delle accortezze di base è quella di scegliere <strong>siti ufficiali da cui scaricare il materiale</strong> gratuito. Ma esistono anche tecniche a portata di mano che permettono di migliorare la sicurezza in WordPress. Sono in realtà tecniche che si consigliano per qualsiasi sito: nascondere le informazioni che non devono essere mostrare, impedire l’accesso in determinate zone, ecc.</p>
<p>Vediamo la lista, che ho recuperato dopo uno studio in rete su diversi siti specifici.</p>
<h3 style="margin-top: 50px; margin-bottom: 20px; color: #c00;">Permessi, prefisso delle tabelle, username e password sicure</h3>
<p>Partiamo dalle basi.<br />
Prima di tutto, è buona norma <strong>modificare il prefisso delle tabelle</strong> che WordPress ci propone (&#8220;wp_&#8221;). In questo modo l’intruso, se raggiunge il database, non può conoscere il nome esatto delle tabelle da estrarre. Se il danno è già fatto, cioè avete installato il cms mantenendo il prefisso tabelle proposto, potete modificarlo in un secondo momento agendo direttamente nel database e modificando il nome di ogni singola tabella con questa query:</p>
<div class="wp_syntax">
<table>
<tbody>
<tr>
<td class="line_numbers">
<pre>1</pre>
</td>
<td class="code">
<pre class="mysql" style="font-family: monospace;"><span style="color: #990099; font-weight: bold;">RENAME</span> <span style="color: #990099; font-weight: bold;">table</span> <span style="color: #008000;">'nomeTabella'</span> <span style="color: #990099; font-weight: bold;">to</span> <span style="color: #008000;">'nuovoNome'</span></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p><span id="more-1910"></span></p>
<p>Seconda cosa, fondamentale è la <strong>modifica di username e l&#8217;uso di password sicure</strong>. Non usate il classico username &#8220;admin&#8221; per entrare, usatene uno personalizzato, e adoperate password possibilmente complesse (le solite raccomandazioni: uso combinato di numeri, lettere maiuscole e minuscole e simboli; soprattutto, non usate parole che si possono recuperare dal comune vocabolario).</p>
<p>Ancora meglio sarebbe modificare l&#8217;id del record che corrisponde all&#8217;utente amministratore. Poiché è il primo utente creato, l&#8217;admin avrà un ID=1 . Una cosa semplice è assegnargli nella tabella del database un ID differente.</p>
<p>Infine, è buona cosa <strong>modificare i permessi dei singoli file e cartelle</strong>: tolte particolari esigenze, i permessi dei file dovrebbero essere settati a 644, mentre quelli delle cartelle a 755.</p>
<h3 style="margin-top: 50px; margin-bottom: 20px; color: #c00;">Impedire l&#8217;accesso ai file più importanti</h3>
<p>Il file <em>config.php</em> contiene i parametri di accesso al database. Se un cracker riesce ad aprirlo, avrà libero accesso a ogni vostro dato. Il modo più semplice per <strong>impedire l&#8217;accesso al file di configurazione</strong> è tramite l&#8217;htaccess. Aprite il file <em>.htaccess</em> presente nella root del sito e inserite queste righe (fatevi sempre un backup dell&#8217;htaccess, per eventuali malfunzionamenti):</p>
<div class="wp_syntax">
<table>
<tbody>
<tr>
<td class="line_numbers">
<pre>1
2
3
4</pre>
</td>
<td class="code">
<pre class="text" style="font-family: monospace;">&lt;files wp-config.php&gt;
order allow,deny
deny from all
&lt;/files&gt;</pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>Dopodiché, nessuno potrà accedervi direttamente attraverso l&#8217;uso di un codice.</p>
<p><script type="text/javascript">// <![CDATA[
google_ad_client = "ca-pub-9984963620559913"; /* Sir Bit - post singolo rettangolare */ google_ad_slot = "5334366715"; google_ad_width = 336; google_ad_height = 280;
// ]]&gt;</script><br />
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">// <![CDATA[


// ]]&gt;</script></p>
<p>Un altro file &#8220;caldo&#8221; è quello di login, che un malintenzionato potrebbe modificare, <strong>dirottando la destinazione dell&#8217;accesso verso una sua pagina</strong> e catturandovi così i dati immessi (username e password). Se accedete sempre con lo stesso indirizzo IP, inserite nell&#8217;<em>htaccess</em> queste righe (avendo cura di modificare gli ultimi numeri con il vostro indirizzo IP):</p>
<div class="wp_syntax">
<table>
<tbody>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5</pre>
</td>
<td class="code">
<pre class="text" style="font-family: monospace;">&lt;files wp-login.php&gt;
Order deny,allow
Deny from All
Allow from 262.263.264.0
&lt;/files&gt;</pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>Ancora, è utile impedire l&#8217;accesso a tutte quelle cartelle che sono prive del file &#8220;index.php&#8221; (e che quindi non dovrebbero mai essere direttamente accessibili). All&#8217;inizio dell&#8217;<em>htaccess</em> scrivete:</p>
<div class="wp_syntax">
<table>
<tbody>
<tr>
<td class="line_numbers">
<pre>1</pre>
</td>
<td class="code">
<pre class="text" style="font-family: monospace;">Options -Indexes</pre>
</td>
</tr>
</tbody>
</table>
</div>
<h3 style="margin-top: 50px; margin-bottom: 20px; color: #c00;">Protezioni più avanzate</h3>
<p>WordPress mette a disposizione delle <strong>chiavi segrete per crittografare i cookies</strong>. Per inserirne di personalizzate, aprite il file <em>config.php</em> e cercate la riga dove viene definita la costante (al posto di &#8220;stringa_di_sicurezza&#8221; troverete una lunga stringa alfanumerica e simbolica):</p>
<div class="wp_syntax">
<table>
<tbody>
<tr>
<td class="line_numbers">
<pre>1</pre>
</td>
<td class="code">
<pre class="php" style="font-family: monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'AUTH_KEY'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'stringa_di_sicurezza'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>La <em>stringa_di_sicurezza</em> deve essere sostituita con quella che potete recuperare al seguente indirizzo:<br />
https://api.wordpress.org/secret-key/1.1/salt/</p>
<p>Un sistema ancora più efficiente in campo di sicurezza è <strong>l&#8217;uso del protocollo https.</strong> Il vostro dominio deve poter disporre di un certificato SSL, naturalmente, ma accedere nel pannello di amministrazione via &#8220;https&#8221; (invece che &#8220;http&#8221;) significa che i dati da voi inseriti saranno crittografati e pertanto più difficili da recuperare da un cracker.</p>
<div style="margin-top: 100px; margin-bottom: 20px;"><strong><em>Fonti esterne</em></strong></div>
<p><a title="Un ottimo articolo su Your Inspiration Web sulla sicurezza in WordPress" href="http://www.yourinspirationweb.com/2013/01/23/come-aumentare-la-sicurezza-di-wordpress/" target="_blank">Un ottimo articolo su Your Inspiration Web sulla sicurezza in WordPress</a></p>The post <a href="https://www.manuelmarangoni.it/sir-bit/1910/wordpress-rendere-sicuro-il-sito-dagli-attacchi-di-hacker/">WordPress – Rendere sicuro il sito dagli attacchi di hacker</a> first appeared on <a href="https://www.manuelmarangoni.it/sir-bit">Sir Bit</a>.]]></content:encoded>
					
					<wfw:commentRss>https://www.manuelmarangoni.it/sir-bit/1910/wordpress-rendere-sicuro-il-sito-dagli-attacchi-di-hacker/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
