<?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>ACCES INTERZIS &#187; MySQL</title>
	<atom:link href="http://www.accesinterzis.ro/index.php/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.accesinterzis.ro</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Thu, 14 Oct 2010 01:05:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Cum fac un formular de autentificare a utilizatorilor?</title>
		<link>http://www.accesinterzis.ro/index.php/programare/cum-fac-un-formular-de-autentificare-a-utilizatorilor/</link>
		<comments>http://www.accesinterzis.ro/index.php/programare/cum-fac-un-formular-de-autentificare-a-utilizatorilor/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 18:54:31 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[programare]]></category>
		<category><![CDATA[backend]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.accesinterzis.ro/?p=246</guid>
		<description><![CDATA[UPDATED ARTICLE
Un website (website de prezentare, blog, forum, magazin online, portal) este alcatuit din doua parti:

frontend &#8211; este  partea publica a unui website unde se gaseste tot continutul lui (text, imagini, video, animatii Flash). Frontend-ul este destinat vizitatorilor. De exemplu, frontend-ul unui website de prezentare al unei firme este constituit din toate paginile sale cum [...]


<strong>Articole asemanatoare:<ul><li><a href='http://www.accesinterzis.ro/index.php/web/cum-fac-un-formular-de-autentificare-a-utilizatorilor-versiunea-2/' rel='bookmark' title='Permanent Link: Cum fac un formular de autentificare a utilizatorilor? (versiunea 2)'>Cum fac un formular de autentificare a utilizatorilor? (versiunea 2)</a></li>
<li><a href='http://www.accesinterzis.ro/index.php/programare/cum-fac-un-formular-de-inregistrare-a-utilizatorilor/' rel='bookmark' title='Permanent Link: Cum fac un formular de inregistrare a utilizatorilor?'>Cum fac un formular de inregistrare a utilizatorilor?</a></li>
<li><a href='http://www.accesinterzis.ro/index.php/web/cum-fac-un-formular-de-inregistrare-a-utilizatorilor-versiunea-2/' rel='bookmark' title='Permanent Link: Cum fac un formular de inregistrare a utilizatorilor? (versiunea 2)'>Cum fac un formular de inregistrare a utilizatorilor? (versiunea 2)</a></li>
</ul>]]></description>
			<content:encoded><![CDATA[<p><span style="color: #ff0000;"><strong>UPDATED ARTICLE</strong></span></p>
<p>Un website (website de prezentare, blog, forum, magazin online, portal) este alcatuit din doua parti:</p>
<ul>
<li><strong>frontend</strong> &#8211; este  partea publica a unui website unde se gaseste tot continutul lui (text, imagini, video, animatii Flash). Frontend-ul este destinat <strong>vizitatorilor</strong>. De exemplu, frontend-ul unui website de prezentare al unei firme este constituit din toate paginile sale cum ar fi pagina &#8220;Despre&#8221; (unde firma prezinta istoricul firmei si profilul de activitate al firmei), pagina &#8220;Portofoliu&#8221; (unde firma prezinta lucrarile realizate si proiectele in curs de realizare), pagina &#8220;Galerie&#8221; (unde vizitatorul gaseste o galerie de imagini cu produsele firmei), pagina &#8220;Contact&#8221; (unde vizitatorul poate intra in legatura cu detinatorii firmei) s.a.m.d..</li>
<li><strong>backend</strong> &#8211; este partea cu acces restrictionat din spatele websiteului pe care numai un numar limitat de <strong>utilizatori</strong> o pot accesa pe baza unei autentificari. Din backend se administreaza continutul de pe frontend si chiar se pot face modificari asupra designului, structurii si functionalitatii frontend-ului in sine. Spre deosebire de frontend, backend-ul nu trebuie sa fie indexat de motoarele de cautare (Google, Yahoo, Bing, Ask.com etc.). Ca sa evitam acest lucru vom scrie in fisierul robots.txt din folderul radacina a websiteului urmatoarele linii:
<pre class="brush: plain;">
User-agent: *
Disallow: /backend/
</pre>
</li>
</ul>
<p>In loc de termenul <strong>backend</strong> se mai folosesc termenii <strong>sectiune de administrare</strong> si <strong>CMS </strong>(de la Content Managment System &#8211; in limba engleza, Sistem de gestionare a continutului). Cand intalnim unul din acesti termeni trebuie sa stim ca se face referire la unul si acelasi lucru.</p>
<p>Ca sa restrictionam accesul la sectiunea de administrare <a title="Cum fac un formular de inregistrare a utilizatorilor unei sectiuni de administrare?" href="http://www.accesinterzis.ro/index.php/programare/cum-fac-un-formular-de-inregistrare-a-utilizatorilor/" target="_blank">trebuie mai intai sa inregistram toti utilizatorii intr-o baza de date</a>. Apoi cream un <a title="Cum fac un formular de autentificare a utilizatorilor?" href="http://www.accesinterzis.ro/myportofolio/loginf-v.2.0.php" target="_blank">formular de autentificare</a> a utilizatorilor sectiunii de administrare si un tabel, numit <strong>ai_authentication_logs</strong>, in care vom inregistra loguri despre autentificarile facute.</p>
<pre class="brush: php;">
$q = &quot;CREATE TABLE IF NOT EXISTS ai_authentication_logs(
		authentication_log_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
		username VARCHAR(30) NOT NULL,
		password VARCHAR(40) NOT NULL,
		server_authentication_date DATETIME NOT NULL,
		client_authentication_date DATETIME NOT NULL,
		ip VARCHAR(30) NOT NULL,
		browser_os VARCHAR(255) NOT NULL,
		screen_resolution VARCHAR(15) NOT NULL,
		status VARCHAR(10) NOT NULL,
		PRIMARY KEY(authentication_log_id))&quot;;

mysql_query($q) or die(mysql_error());
</pre>
<p><span id="more-246"></span></p>
<p>Acest tabel ne permite sa tinem o evidenta a celor care utilizeaza sectiunea de administrare. De fiecare data cand cineva se autentifica se introduce in baza de date numele utilizatorului, parola encriptata a utilizatorului, data si ora autentificarii (atat ora de pe server cat si ora de pe PC-ul utilizatorului), IP-ul de pe care s-a facut autentificarea, browserul si sistemul de operare folosite de utilizator, rezolutia monitorului utilizatorului, statusul autentificarii (reusita sau esuata).</p>
<p>Daca o autentificare esueaza parola va fi inregistrata in clar. Astfel putem vedea daca cineva doreste sa intre neautorizat in sectiunea de administrare si cu ce cuvinte incearca sa sparga parola. Se stie foarte bine ca o parola poate fi aflata prin forta bruta (<strong>brute force attack</strong>), utilizand un dictionar de cuvinte (<strong>dictionary attack</strong>) sau prin inginerie sociala (<strong>social engineering</strong>) sau poate fi pur si simplu ghicita. De asemenea un utilizator poate sa isi infecteze PC-ul cu un <strong>keylogger</strong> si astfel un rau-voitor sa intre in posesia datelor sale de logare.</p>
<p>Mai pe scurt, acest tabel ne ajuta sa vedem daca se intampla ceva suspicios in legatura cu utilizatorii sectiunii de administrare.</p>
<p>In cazul in care autentificarea reuseste, se va crea o sesiune pe server, se va salva in sesiune numele utilizatorului si browserul si sistemul de operare folosite de utilizator si se va face redirectionarea de la pagina de login la pagina principala a sectiunii de administrare.</p>
<p>Apoi pe pagina principala a sectiunii de administrare, deasupra DOCTYPE-ului mai exact, exista un script PHP care verifica daca cel care vrea sa deschida pagina este autorizat sau nu sa faca acest lucru. Daca este autorizat pagina va fi afisata in browser, daca nu este autorizat va fi redirectionat catre pagina de login. Acest script PHP il punem in toate paginile sectiunii de administrare, deasupra DOCTYPE-ului, pentru a restrictiona accesul la ele.</p>
<p>Se prefera utilizarea sesiunilor in detrimentul cookie-urilor deoarece datele de logare ale utilizatorului sunt stocate pe server si nu pe PC-ul utilizatorului. In acest fel datele de logare sunt infinit mai protejate si nu sunt transmise in mod repetat intre server si browser. Pe PC-ul utilizatorului se creeaza doar un cookie cu identificatorul sesiunii. Numele acestui cookie este <strong>PHPSESSID</strong> si valoarea stocata de cookie este o valoare de genul <strong>a76b45cf92d87ea710fc8e9a9f812fa</strong> (32 de caractere hexazecimale). Existenta acestui cookie se poate verifica din browser.</p>
<p>Pentru ca acest script PHP de autentificare sa fie complet functional mai am nevoie de <a title="Cum ma conectez la o baza de date MySQL?" href="http://www.accesinterzis.ro/index.php/programare/cum-ma-conectez-la-o-baza-de-date/" target="_blank">fisierul care ma conecteaza la baza de date</a>, <strong>connect_to_db.inc.php</strong>, pe care il apelez cu functia include().</p>
<p><strong>loginform.php</strong> &#8211; pagina cu formularul de autentificare</p>
<pre class="brush: php;">
&lt;?php
/*
Titlu: Cum fac un formular de autentificare a utilizatorilor?
Autor: Marian Barbu aka AccesInterzis
Website: http://www.accesinterzis.ro
2010 (c) Toate drepturile rezervate
*/

//-----creez o sesiune pe server pentru a salva in ea, in caz ca autentificarea reuseste, numele utilizatorului si browserul si sistemul de operare folosite de utilizator
#1
session_start();

//-----ma conectez la baza de date
#2
include('includes/connect_to_db.inc.php');

//-----infasor in strip_tags() si htmlentities() URL-urile obtinute dinamic si cookie-urile ca sa ma asigur ca nu contin cod malitios
#3
$php_self = htmlentities(strip_tags($_SERVER['PHP_SELF']), ENT_QUOTES, 'utf-8');
$referer = (isset($_SERVER['HTTP_REFERER'])) ? htmlentities(strip_tags($_SERVER['HTTP_REFERER']), ENT_QUOTES, 'utf-8') : NULL;

$cookie = array();
foreach ($_COOKIE as $k =&gt; $v) {
	$v = htmlentities(strip_tags($v), ENT_QUOTES, 'utf-8');

	$cookie[$k] = $v;
}

//-----specific EXACT cu ce campuri se va lucra
#4
$required_fields = array('username', 'password', 'login');
$sent_fields = array_keys($_POST);

//------scriptul PHP se executa doar daca cererea a fost facuta de pe aceeasi pagina pe care se afla formularul si doar daca toate campurile formularului au fos trimise
#5
if ($referer == 'http://'.$_SERVER['HTTP_HOST'].$php_self &amp;&amp; $required_fields == $sent_fields) {
	//-----initializez array-ul in care voi stoca mesajele de eroare si array-ul in care voi pasa datele din $_POST dupa ce le filtrez
	#5.1
	$errors = array();
	$post = array();

	#5.2
	//Starting data validation
	if (empty($_POST['username'])) {
		$errors['username'] = 'You forgot to enter the &lt;strong&gt;username&lt;/strong&gt;.';
	} else {
		$post['username'] = trim($_POST['username']);

		if (ini_get('magic_quotes_gpc')) {
			$post['username'] = stripslashes($post['username']);
		}

		if (strlen($post['username']) &lt; 3) {
			$errors['username'] = 'The &lt;strong&gt;username is too short&lt;/strong&gt;.';
		} else {
			if (strlen($post['username']) &gt; 30) {
				$errors['username'] = 'The &lt;strong&gt;username&lt;/strong&gt; is too long.';
			} else {
				if (!preg_match('/[a-z0-9_ ]*/i', $post['username'])) {
					$errors['username'] = 'The &lt;strong&gt;username&lt;/strong&gt; isn\'t valid.';
				}
			}
		}
	}

	if (empty($_POST['password'])) {
		$errors['password'] = 'You forgot to enter the &lt;strong&gt;password&lt;/strong&gt;.';
	} else {
		$post['password'] = trim($_POST['password']);

		if (ini_get('magic_quotes_gpc')) {
			$post['password'] = stripslashes($post['password']);
		}

		if (strlen($post['password']) &lt; 5) {
			$errors['password'] = 'The &lt;strong&gt;password&lt;/strong&gt; is too short.';
		} else {
			if (strlen($post['password']) &gt; 30) {
				$errors['password'] = 'The &lt;strong&gt;password&lt;/strong&gt; is too long.';
			} else {
				if (!preg_match('/^[a-z0-9][a-z0-9_ ]*[a-z0-9]$/i', $post['password'])) {
					$errors['password'] = 'The &lt;strong&gt;password&lt;/strong&gt; isn\'t valid.';
				}
			}
		}
	}

	//------daca nu exista niciun fel de erori bag datele in baza de date
    #4.3
	if (count($errors) == 0) {
		//-----verific daca datele de logare exista si in baza de date
		#4.3.1
		$q = &quot;SELECT username FROM ai_registrationform WHERE username='&quot;.$post['username'].&quot;' AND password=SHA('&quot;.$post['password'].&quot;')&quot;;
		$result = mysql_query($q) or die(mysql_error());
		$row = mysql_fetch_array($result);

		/*
		Daca autentificarea s-a efetuat cu succes:
		- salvez in tabelul de loguri un log despre autetificare
		- salvez in sesiune numele utilizatorului si numele browserului si sistemului de operare pe care le foloseste
		- redirectez utilizatorul de pe pagina de login pe pagina principala a sectiunii de administrare
		*/
		if ($row) {
			$q2 = &quot;INSERT INTO ai_authentication_logs(`username`, `password`, `server_authentication_date`, `client_authentication_date`, `ip`, `browser_os`, `screen_resolution`, `status`)&quot;
				 .&quot;VALUES('&quot;.$post['username'].&quot;', SHA('&quot;.$post['password'].&quot;'), NOW(), '&quot;.$cookie['client_authentication_date'].&quot;', '&quot;.$_SERVER['REMOTE_ADDR'].&quot;', '&quot;.$_SERVER['HTTP_USER_AGENT'].&quot;', '&quot;.$cookie['screen_resolution'].&quot;', 'succesful')&quot;;

			mysql_query($q2) or die(mysql_error());

			$_SESSION['username'] = $row['username'];
			$_SESSION['browser_os'] = sha1($_SERVER['HTTP_USER_AGENT']);

			header('Location:http://'.$_SERVER['HTTP_HOST'].dirname($php_self).'/backend.php');
			exit();
		} else {
			$q2 = &quot;INSERT INTO ai_authentication_logs(`username`, `password`, `server_authentication_date`, `client_authentication_date`, `ip`, `browser_os`, `screen_resolution`, `status`)&quot;
				 .&quot;VALUES('&quot;.$post['username'].&quot;', '&quot;.$post['password'].&quot;', NOW(), '&quot;.$cookie['client_authentication_date'].&quot;', '&quot;.$_SERVER['REMOTE_ADDR'].&quot;', '&quot;.$_SERVER['HTTP_USER_AGENT'].&quot;', '&quot;.$cookie['screen_resolution'].&quot;', 'failed')&quot;;

			mysql_query($q2) or die(mysql_error());

			$error_message = 'Your login data are wrong.';
			$errors['username'] = '';
			$errors['password'] = '';
		}
	}

	//-----infasor datele in htmlentities() deoarece urmeaza sa le afisez in formular
	#4.4
	foreach ($post as $k =&gt; $v) {
		$post[$k] = htmlentities(stripslashes($v), ENT_QUOTES, 'utf-8');
	}
}
?&gt;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;How do I make a login form?&lt;/title&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
&lt;meta name=&quot;description&quot; content=&quot;How do I make a login form?&quot; /&gt;
&lt;meta name=&quot;keywords&quot; content=&quot;login,form,php,script,mysql,database,user,backend,admin,section,cms&quot; /&gt;
&lt;meta name=&quot;abstract&quot; content=&quot;How do I make a login form?&quot; /&gt;
&lt;meta name=&quot;author&quot; content=&quot;AccesInterzis&quot; /&gt;
&lt;meta name=&quot;copyright&quot; content=&quot;AccesInterzis&quot; /&gt;
&lt;meta name=&quot;robots&quot; content=&quot;index,follow&quot; /&gt;
&lt;meta name=&quot;revisit-after&quot; content=&quot;7 days&quot; /&gt;

&lt;style type=&quot;text/css&quot;&gt;
* {
	margin:0;
	padding:0;
	outline:none;
}

html {
	color:black;
	background-color:white;
	font: normal normal normal 12px  Verdana;
	/*font-style font-variant font-weight font-size font-family*/
}

/*INCEPUT - LINIILE CSS CARE CREEAZA SKINUL FORMULARULUI DE AUTENTIFICARE*/

div#loginf {
	width:285px;
	margin:0px auto;
}

div#loginf h1 {
	color:black;
	font: normal normal normal 24px  Verdana;
	/*font-style font-variant font-weight font-size font-family*/
	padding-bottom:5px;
}

div#loginf div {
	margin:0 0 5px 0;
}

div#loginf label {
	width:80px;
	float:left;
}

div#loginf label span {
	color:#c00;
}

div#loginf input {
	width:200px;
}

div#loginf textarea {
	width:300px;
	height:150px;
}

div#loginf input, div#loginf textarea {
	border:1px #ccc solid;
}

div#loginf input:hover, div#loginf textarea:hover {
	border:1px #666 solid;
}

div#loginf input#login {
	width:auto;
	color:#FFF;
	background-color:#333;
	border:1px #000 solid !important;
	cursor:pointer;
}

div#loginf input#login:hover {
	color:#333;
	background-color:#fff;
	border:1px #333 solid;
}

/*Inceput - stilurile erorilor*/
div#loginf p {
	color:#c00;
	padding:0 0 0 80px;
	font-size:10px;
	text-align:left;
}

div#loginf div#username_field label,
div#loginf div#password_field label {
	color:#c00;
}

div#loginf div#username_field input,
div#loginf div#password_field input {
	border:1px #c00 solid;
	color:#c00;
}

div#loginf div#username_field input:hover,
div#loginf div#password_field input:hover {
	border:1px #c00 solid;
}
/*Sfarsit - stilurile erorilor*/

/*SFARSIT - LINIILE CSS CARE CREEAZA SKINUL FORMULARULUI DE AUTENTIFICARE*/
&lt;/style&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
/*
Creez cookie-urile care vor stoca date despre utilizator.
Aceste cookie-uri se creeaza atunci cand se deschide prima oara pagina de login.
De abia cand se reincarca pagina, adica cand utilizatorul apasa butonul &quot;login&quot;, scriptul PHP
se poate folosi de aceste cookie-uri.
*/
document.cookie = 'screen_resolution=' + screen.width + '*' + screen.height + ';';

var current_date = new Date();
var year = current_date.getFullYear();
var month = current_date.getMonth();
var day = current_date.getDate();
var hour = current_date.getHours();
var minutes = current_date.getMinutes();
var seconds = current_date.getSeconds();
var current_date = year + '-' + month + '-' + day + ' ' + hour + ':' + minutes + ':' + seconds;
document.cookie = 'client_authentication_date=' + current_date + ';';
&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;div id=&quot;loginf&quot;&gt;
	&lt;h1&gt;
		&lt;label&gt;&amp;nbsp;&lt;/label&gt;
		Login
	&lt;/h1&gt;

	&lt;?php if (isset($error_message)) echo '&lt;p&gt;'.$error_message.'&lt;/p&gt;'; ?&gt;

	&lt;form action=&quot;&lt;?php echo 'http://'.$_SERVER['HTTP_HOST'].$php_self; ?&gt;&quot; method=&quot;post&quot;&gt;
		&lt;?php echo (isset($errors['username'])) ? '&lt;p&gt;'.$errors['username'].'&lt;/p&gt;&lt;div id=&quot;username_field&quot;&gt;' : '&lt;div&gt;' ; ?&gt;
			&lt;label for=&quot;username&quot;&gt;Username&lt;span&gt;*&lt;/span&gt;:&lt;/label&gt;
			&lt;input name=&quot;username&quot; type=&quot;text&quot; id=&quot;username&quot; value=&quot;&lt;?php if (isset($post['username'])) echo $post['username']; ?&gt;&quot; /&gt;
		&lt;/div&gt;

		&lt;?php echo (isset($errors['password'])) ? '&lt;p&gt;'.$errors['password'].'&lt;/p&gt;&lt;div id=&quot;password_field&quot;&gt;' : '&lt;div&gt;' ; ?&gt;
			&lt;label for=&quot;password&quot;&gt;Password&lt;span&gt;*&lt;/span&gt;:&lt;/label&gt;
			&lt;input name=&quot;password&quot; type=&quot;password&quot; id=&quot;password&quot; /&gt;
		&lt;/div&gt;

		&lt;div&gt;
			&lt;label&gt; &amp;nbsp; &lt;/label&gt;
			&lt;input name=&quot;login&quot; type=&quot;submit&quot; id=&quot;login&quot; value=&quot;login&quot; /&gt;
		&lt;/div&gt;
	&lt;/form&gt;
&lt;/div&gt;
&lt;/html&gt;
</pre>
<p><strong> backend.php</strong> &#8211; pagina principala a sectiunii de administrare</p>
<pre class="brush: php;">
&lt;?php
#1
session_start();
$php_self = htmlentities(strip_tags($_SERVER['PHP_SELF']), ENT_QUOTES, 'utf-8');
$browser_os = htmlentities(strip_tags($_SERVER['HTTP_USER_AGENT']), ENT_QUOTES, 'utf-8');

#2
if (!isset($_SESSION['username']) || !isset($_SESSION['browser_os']) || $_SESSION['browser_os'] != sha1($browser_os)) {
		header('Location:http://'.$_SERVER['HTTP_HOST'].dirname($php_self).'/loginf.php');
		exit();
}

#3
if (isset($_GET['action']) &amp;&amp; $_GET['action'] == 'logout') {
	#3.1
	/*
	Pentru a deloga un utilizator din sectiunea de administrare trebuie sa urmez pasii de mai jos:
	- distrug toate variabilele sesiunii de pe server reinitializind intregul tablou superglobal $_SESSION
	- sterg de pe server toate datele sesiunii apeland functia session_destroy()
	- sterg de pe PC-ul utilizatorului cookie-ul care stocheaza identificatorul de sesiune
	*/
	$_SESSION = array();
	if (session_destroy() &amp;&amp; setcookie('PHPSESSID', '', time()-300, '/', '', 0)) {
		header('Location:http://'.$_SERVER['HTTP_HOST'].dirname($php_self).'/loginf.php');
		exit();
	}
}
?&gt;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Backend&lt;/title&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
&lt;meta name=&quot;description&quot; content=&quot;How do I make a backend?&quot; /&gt;
&lt;meta name=&quot;keywords&quot; content=&quot;backend,administration,section,admin,cms,content,managament,system,php,script,mysql,database&quot; /&gt;
&lt;meta name=&quot;abstract&quot; content=&quot;How do I make a backend?&quot; /&gt;
&lt;meta name=&quot;author&quot; content=&quot;AccesInterzis&quot; /&gt;
&lt;meta name=&quot;copyright&quot; content=&quot;AccesInterzis&quot; /&gt;
&lt;meta name=&quot;robots&quot; content=&quot;index,follow&quot; /&gt;
&lt;meta name=&quot;revisit-after&quot; content=&quot;7 days&quot; /&gt;

&lt;link href=&quot;http://www.accesinterzis.ro/myportofolio/css/reset.css&quot; type=&quot;text/css&quot; rel=&quot;stylesheet&quot; media=&quot;all&quot; /&gt;
&lt;style type=&quot;text/css&quot;&gt;
* {
	margin:0;
	padding:0;
	outline:none;
}

html {
	color:black;
	background-color:white;
	font: normal normal normal 12px  Verdana;
	/*font-style font-variant font-weight font-size font-family*/
}

/*INCEPUT - LINIILE CSS CARE CREEAZA SKINUL SECTIUNII DE ADMINISTRARE*/

div#backend {
	width:1000px;
	margin:10px auto;
	overflow:auto;
	border:1px #ccc solid;
	padding:10px;
}

div#backend a {
	color:#900;
	text-decoration:none;
}

div#backend ul#welcome_message {
	overflow:auto;
	list-style-type:none;
}

div#backend ul#sidebar {
	width:10%;
	float:left;
	list-style-type:none;
	border-right:1px #ccc solid;
}

div#backend div#mainarea {
	width:80%;
	float:right;
}

div#backend div#mainarea p#confirmation_message {
	width:75%;
	margin:250px auto;
}

div#backend div#footer{
	width:100%;
	float:left;
	text-align:center;
}

div.splitter {
	width:100%;
	height:1px;
	clear:both;
	float:left;
	border-top:1px #ccc solid;
	margin: 10px 0 10px 0;
}

/*SFARSIT - LINIILE CSS CARE CREEAZA SKINUL SECTIUNII DE ADMINISTRARE*/
&lt;/style&gt;
&lt;/head&gt;

&lt;body&gt;

&lt;div id=&quot;backend&quot;&gt;
	&lt;ul id=&quot;welcome_message&quot;&gt;
		&lt;li style=&quot;float:left;&quot;&gt;Welcome &lt;strong&gt;&lt;?php echo $_SESSION['username']; ?&gt;&lt;/strong&gt; to the administration section&lt;/li&gt;
		&lt;li style=&quot;float:right;&quot;&gt;&lt;a href=&quot;&lt;?php echo 'http://'.$_SERVER['HTTP_HOST'].$php_self; ?&gt;?action=logout&quot; title=&quot;Logout&quot;&gt;Logout&lt;/a&gt;&lt;/li&gt;
	&lt;/ul&gt;

	&lt;div class=&quot;splitter&quot;&gt;&lt;/div&gt;

	&lt;ul id=&quot;sidebar&quot;&gt;
		&lt;?php
		for ($i = 0; $i &lt; 40; $i++) {
			echo '&lt;li&gt;sidebar&lt;/li&gt;';
		}
		?&gt;
	&lt;/ul&gt;

	&lt;div id=&quot;mainarea&quot;&gt;
		&lt;p id=&quot;confirmation_message&quot;&gt;
		&lt;strong&gt;&lt;?php echo $_SESSION['username']; ?&gt;&lt;/strong&gt;, esti aici deoarece ai dovedit ca esti un utilizator autorizat al acestei sectiuni de administrare.
		Acum delogheaza-te si incearca sa accesezi din nou &lt;strong&gt;&lt;?php echo 'http://'.$_SERVER['HTTP_HOST'].$php_self; ?&gt;&lt;/strong&gt;.
		Vei vedea ce se intampla cand cineva neautentificat incearca sa intre in sectiunea de administrare.
		&lt;/p&gt;
	&lt;/div&gt;

	&lt;div class=&quot;splitter&quot;&gt;&lt;/div&gt;

	&lt;div id=&quot;footer&quot;&gt;
		Designed and developed by &lt;a href=&quot;http://www.accesinterzis.ro&quot; title=&quot;Programare | Web development | Web design | Securitate IT | SEO&quot; target=&quot;_blank&quot;&gt;www.accesinterzis.ro&lt;/a&gt; &amp;copy; 2010. All rights reserved.
	&lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>Pentru un mai bun managment al codului trebuie sa facem urmatoarele lucruri:</p>
<ul>
<li>scriptul PHP de deasupra DOCTYPE-ului paginii de login, care valideaza datele introduse in formular, autentifica utilizatorul si il redirectioneaza catre sectiunea de administrare, se baga intr-un fisier include si se apeleaza prin functia PHP include().
<pre class="brush: php;">
&lt;?php @include('includes/authenticate_user.inc.php'); ?&gt;
</pre>
</li>
<li>liniile CSS, care creeaza skinul formularului de autentificare, din sectiunea &lt;head&gt; &lt;/head&gt; a paginii de login se baga intr-un fisier CSS extern si se apeleaza cu tagul HTML &lt;link /&gt;.
<pre class="brush: xml;">
&lt;link href=&quot;css/login_form_design.css&quot; type=&quot;text/css&quot; rel=&quot;stylesheet&quot; media=&quot;all&quot; /&gt;
</pre>
</li>
<li>liniile Javascript, care creeaza cookie-urile cu datele despre utilizator, din sectiunea &lt;head&gt;&lt;/head&gt; a paginii de login se baga intr-un fisier JS extern si se apeleaza cu tagul HTML &lt;script&gt;.
<pre class="brush: xml;">
&lt;script type=&quot;text/javascript&quot; src=&quot;js/user_data.js&quot;&gt;&lt;/script&gt;
</pre>
</li>
<li>scriptul PHP de deasupra DOCTYPE-ului paginilor ce alcatuiesc sectiunea de administrare, care permite doar utilizatorilor autentificati sa intre in sectiunea de administrare si realizeaza si functia de logout, se baga intr-un fisier include si se apeleaza prin functia PHP include().
<pre class="brush: php;">
&lt;?php @include('includes/restricted_area.inc.php'); ?&gt;
</pre>
</li>
<li>liniile CSS, care creeaza skinul sectiunii de administrare, din sectiunea &lt;head&gt; &lt;/head&gt;se baga intr-un fisier CSS extern si se apeleaza cu tagul HTML &lt;link /&gt;.
<pre class="brush: xml;">
&lt;link href=&quot;css/backend_design.css&quot; type=&quot;text/css&quot; rel=&quot;stylesheet&quot; media=&quot;all&quot; /&gt;
</pre>
</li>
</ul>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Cum+fac+un+formular+de+autentificare+a+utilizatorilor%3F+http://qkzmc.th8.us" title="Publica acest articol pe Twitter"><img class="nothumb" src="http://www.accesinterzis.ro/wp-content/plugins/tweet-this/icons/tt-twitter.png" alt="Post to Twitter" /></a> <a class="tt" href="http://twitter.com/home/?status=Cum+fac+un+formular+de+autentificare+a+utilizatorilor%3F+http://qkzmc.th8.us" title="Publica acest articol pe Twitter">Publica acest articol pe Twitter</a></p>

<p><strong>Articole asemanatoare:<ul><li><a href='http://www.accesinterzis.ro/index.php/web/cum-fac-un-formular-de-autentificare-a-utilizatorilor-versiunea-2/' rel='bookmark' title='Permanent Link: Cum fac un formular de autentificare a utilizatorilor? (versiunea 2)'>Cum fac un formular de autentificare a utilizatorilor? (versiunea 2)</a></li>
<li><a href='http://www.accesinterzis.ro/index.php/programare/cum-fac-un-formular-de-inregistrare-a-utilizatorilor/' rel='bookmark' title='Permanent Link: Cum fac un formular de inregistrare a utilizatorilor?'>Cum fac un formular de inregistrare a utilizatorilor?</a></li>
<li><a href='http://www.accesinterzis.ro/index.php/web/cum-fac-un-formular-de-inregistrare-a-utilizatorilor-versiunea-2/' rel='bookmark' title='Permanent Link: Cum fac un formular de inregistrare a utilizatorilor? (versiunea 2)'>Cum fac un formular de inregistrare a utilizatorilor? (versiunea 2)</a></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://www.accesinterzis.ro/index.php/programare/cum-fac-un-formular-de-autentificare-a-utilizatorilor/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Cum fac un formular de inregistrare a utilizatorilor?</title>
		<link>http://www.accesinterzis.ro/index.php/programare/cum-fac-un-formular-de-inregistrare-a-utilizatorilor/</link>
		<comments>http://www.accesinterzis.ro/index.php/programare/cum-fac-un-formular-de-inregistrare-a-utilizatorilor/#comments</comments>
		<pubDate>Tue, 02 Mar 2010 14:56:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[programare]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.accesinterzis.ro/?p=206</guid>
		<description><![CDATA[UPDATED ARTICLE
Prin ce se caracterizeaza acest formular de inregistrare a utilizatorilor?

scriptul PHP isi creeaza singur tabelul in care va stoca utilizatorii daca acesta nu exista in baza de date
toate datele introduse sunt validate server-side
scriptul verifica in baza de date daca exista deja numele de utilizator ales si adresa de email a noului utilizator
fiecare mesaj de eroare [...]


<strong>Articole asemanatoare:<ul><li><a href='http://www.accesinterzis.ro/index.php/web/cum-fac-un-formular-de-inregistrare-a-utilizatorilor-versiunea-2/' rel='bookmark' title='Permanent Link: Cum fac un formular de inregistrare a utilizatorilor? (versiunea 2)'>Cum fac un formular de inregistrare a utilizatorilor? (versiunea 2)</a></li>
<li><a href='http://www.accesinterzis.ro/index.php/programare/cum-fac-un-formular-de-autentificare-a-utilizatorilor/' rel='bookmark' title='Permanent Link: Cum fac un formular de autentificare a utilizatorilor?'>Cum fac un formular de autentificare a utilizatorilor?</a></li>
<li><a href='http://www.accesinterzis.ro/index.php/programare/cum-fac-un-formular-de-comentarii/' rel='bookmark' title='Permanent Link: Cum fac un formular de comentarii?'>Cum fac un formular de comentarii?</a></li>
</ul>]]></description>
			<content:encoded><![CDATA[<p><span style="color: #ff0000;"><strong>UPDATED ARTICLE</strong></span></p>
<p>Prin ce se caracterizeaza acest formular de inregistrare a utilizatorilor?</p>
<ul>
<li>scriptul PHP isi creeaza singur tabelul in care va stoca utilizatorii daca acesta nu exista in baza de date</li>
<li>toate datele introduse sunt validate server-side</li>
<li>scriptul verifica in baza de date daca exista deja numele de utilizator ales si adresa de email a noului utilizator</li>
<li>fiecare mesaj de eroare este personalizat pentru fiecare eroare in parte</li>
<li>campurile care contin date invalide sunt evidentiate</li>
<li>scriptul pastreaza datele introduse in campurile formularului atunci cand detecteaza o eroare</li>
<li>designul formularului este creat EXCLUSIV din CSS</li>
<li>de protectie antispam nu avem nevoie deoarece se presupune ca formularul de inregistrare se afla in interiorul unei sectiuni de administrare</li>
</ul>
<p><span id="more-206"></span></p>
<p>Asadar, ca sa realizez un formular de inregistrare a utilizatorilor mai intai creez in baza de date tabelul care va stoca toti utilizatorii.</p>
<pre class="brush: php;">
$q = &quot;CREATE TABLE IF NOT EXISTS ai_registrationform(
		user_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
		username VARBINARY(30) NOT NULL,
		email VARCHAR(30) NOT NULL,
		password CHAR(40) NOT NULL,
		registration_date DATETIME NOT NULL,
		ip VARCHAR(16) NOT NULL,
		PRIMARY KEY(user_id))&quot;;

mysql_query($q) or die(mysql_error());
</pre>
<p>Mai avem nevoie de <a title="Cum ma conectez la o baza de date?" href="http://www.accesinterzis.ro/index.php/programare/cum-ma-conectez-la-o-baza-de-date/" target="_blank">connect_to_db.inc.php</a>, fisierul care ne conecteaza la baza de date.</p>
<pre class="brush: php;">
&lt;?php
/*
Titlu: Cum fac un formular de inregistrare a utilizatorilor?
Autor: Marian Barbu aka AccesInterzis
Website: http://www.accesinterzis.ro
2010 (c) Toate drepturile rezervate
*/

//-----ma conectez la baza de date
#1
include('includes/connect_to_db.inc.php');

//-----infasor in strip_tags() si htmlentities() URL-urile obtinute dinamic ca sa ma asigur ca nu contin cod malitios
#2
$php_self = htmlentities(strip_tags($_SERVER['PHP_SELF']), ENT_QUOTES, 'utf-8');
$referer = (isset($_SERVER['HTTP_REFERER'])) ? htmlentities(strip_tags($_SERVER['HTTP_REFERER']), ENT_QUOTES, 'utf-8') : NULL;

//-----specific EXACT cu ce campuri se va lucra
#3
$required_fields = array('username', 'email', 'password', 'retype_password', 'register');
$sent_fields = array_keys($_POST);

//------scriptul PHP se executa doar daca cererea a fost facuta de pe aceeasi pagina pe care se afla formularul si doar daca toate campurile formularului au fos trimise
#4
if ($referer == 'http://'.$_SERVER['HTTP_HOST'].$php_self &amp;&amp; $required_fields == $sent_fields) {
	//-----initializez array-ul in care voi stoca mesajele de eroare si array-ul in care voi pasa datele din $_POST dupa ce le filtrez
	#5.1
	$errors = array();
	$post = array();

	#5.2
	//Starting data validation
	if (empty($_POST['username'])) {
		$errors['username'] = 'You forgot to enter the &lt;strong&gt;username&lt;/strong&gt;.';
	} else {
		$post['username'] = trim($_POST['username']);

		if (ini_get('magic_quotes_gpc')) {
			$post['username'] = stripslashes($post['username']);
		}

		if (strlen($post['username']) &lt; 3) {
			$errors['username'] = 'The &lt;strong&gt;username&lt;/strong&gt; is too short.';
		} else {
			if (strlen($post['username']) &gt; 30) {
				$errors['username'] = 'The &lt;strong&gt;username&lt;/strong&gt; is too long.';
			} else {
				if (!preg_match('/[a-z0-9_ ]*/i', $post['username'])) {
					$errors['username'] = 'The &lt;strong&gt;username&lt;/strong&gt; isn\'t valid.';
				} else {
					$q = &quot;SELECT user_id FROM ai_registrationform WHERE LOWER(username)='&quot;.strtolower($post['username']).&quot;'&quot;;
                    $result = mysql_query($q) or die(mysql_error());

                    if (mysql_num_rows($result) != 0) {
						$errors['username'] = 'The username &lt;strong&gt;'.$post['username'].'&lt;/strong&gt; already exists in our database.';
					}
				}
			}
		}
	}

	if (empty($_POST['email'])) {
		$errors['email'] = 'You forgot to enter the &lt;strong&gt;email address&lt;/strong&gt;.';
	} else {
		$post['email'] = trim($_POST['email']);

		if (ini_get('magic_quotes_gpc')) {
			$post['email'] = stripslashes($post['email']);
		}

		if (strlen($post['email']) &lt; 3) {
			$errors['email'] = 'The &lt;strong&gt;email address&lt;/strong&gt; is too short.';
		} else {
			if (strlen($post['email']) &gt; 30) {
				$errors['email'] = 'The &lt;strong&gt;email address&lt;/strong&gt; is too long.';
			} else {
				if (!preg_match('/^[a-z0-9][a-z0-9_.]+@[a-z0-9-.]+\.[a-z]{2,4}$/i', $post['email'])) {
					$errors['email'] = 'The &lt;strong&gt;email address&lt;/strong&gt; isn\'t valid.';
				} else {
					$q = &quot;SELECT user_id FROM ai_registrationform WHERE LOWER(email)='&quot;.strtolower($post['email']).&quot;'&quot;;
                    $result = mysql_query($q) or die(mysql_error());

                    if (mysql_num_rows($result) != 0) {
						$errors['username'] = 'The email address &lt;strong&gt;'.$post['email'].'&lt;/strong&gt; already exists in our database.';
					}
				}
			}
		}
	}

	if (empty($_POST['password'])) {
		$errors['password'] = 'You forgot to enter the &lt;strong&gt;password&lt;/strong&gt;.';
	} else {
		$post['password'] = trim($_POST['password']);

		if (ini_get('magic_quotes_gpc')) {
			$post['password'] = stripslashes($post['password']);
		}

		if (strlen($post['password']) &lt; 5) {
			$errors['password'] = 'The &lt;strong&gt;password&lt;/strong&gt; is too short.';
		} else {
			if (strlen($post['password']) &gt; 30) {
				$errors['password'] = 'The &lt;strong&gt;password&lt;/strong&gt; is too long.';
			} else {
				if (!preg_match('/^[a-z0-9][a-z0-9_ ]*[a-z0-9]$/i', $post['password'])) {
					$errors['password'] = 'The &lt;strong&gt;password&lt;/strong&gt; isn\'t valid.';
				} else {
					if ($post['password'] != $_POST['retype_password']) {
						$errors['password'] = 'The two passwords don\'t match.';
						$errors['retype_password'] = '';
					}
				}
			}
		}
	}
	//Ending data validation

	//------daca nu exista niciun fel de erori bag datele in baza de date
    #4.3
	 if(count($errors) == 0) {
		//-----infasor datele in mysql_real_escape_string() deoarece urmeaza sa interoghez baza de date
		#4.3.1
		foreach ($post as $k =&gt; $v) {
			$post[$k] = mysql_real_escape_string($v);
		}

		#4.3.2
		$q = &quot;INSERT INTO ai_registrationform(`username`, `email`, `password`, `registration_date`, `ip`)&quot;
			.&quot;VALUES('&quot;.$post['username'].&quot;', '&quot;.$post['email'].&quot;', SHA('&quot;.$post['password'].&quot;'), NOW(), '&quot;.$_SERVER['REMOTE_ADDR'].&quot;')&quot;;

		#4.3.3
		if (mysql_query($q)) {
			$confirmation = 'The user &lt;strong&gt;'.$post['username'].'&lt;/strong&gt; was succesfully registered in our databse.';
		} else {
			$confirmation = 'Something is wrong with the server. The user &lt;strong&gt;'.$post['username'].'&lt;/strong&gt; wasn\'t registered.';
		}

		$confirmation .= ' &lt;a href=&quot;http://'.$_SERVER['HTTP_HOST'].$php_self.'&quot; title=&quot;Back&quot; id=&quot;back&quot;&gt;Back to registration form&lt;/a&gt;';

		$display = 'style=&quot;display:none;&quot;';
	}

	//-----infasor datele in htmlentities() deoarece urmeaza sa le afisez in formular
	#4.4
	foreach ($post as $k =&gt; $v) {
		$post[$k] = htmlentities(stripslashes($v), ENT_QUOTES, 'utf-8');
	}
}
?&gt;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;How do I make a registration form?&lt;/title&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
&lt;meta name=&quot;description&quot; content=&quot;How do I make a registration form?&quot; /&gt;
&lt;meta name=&quot;keywords&quot; content=&quot;registration,form,php,script,mysql,database,user,backend,admin,section,cms&quot; /&gt;
&lt;meta name=&quot;abstract&quot; content=&quot;How do I make a registration form?&quot; /&gt;
&lt;meta name=&quot;author&quot; content=&quot;AccesInterzis&quot; /&gt;
&lt;meta name=&quot;copyright&quot; content=&quot;AccesInterzis&quot; /&gt;
&lt;meta name=&quot;robots&quot; content=&quot;index,follow&quot; /&gt;
&lt;meta name=&quot;revisit-after&quot; content=&quot;7 days&quot; /&gt;

&lt;style type=&quot;text/css&quot;&gt;
* {
	margin:0;
	padding:0;
	outline:none;
}

html {
	color:black;
	background-color:white;
	font: normal normal normal 12px  Verdana;
	/*font-style font-variant font-weight font-size font-family*/
}

/*INCEPUT - LINIILE CSS CARE CREEAZA SKINUL FORMULARULUI DE INREGISTRARE*/

div#registrationf {
	width:335px;
	margin:0px auto;
}

div#registrationf h1 {
	color:black;
	font: normal normal normal 24px  Verdana;
	/*font-style font-variant font-weight font-size font-family*/
	padding-bottom:5px;
}

div#registrationf div {
	margin:0 0 5px 0;
}

div#registrationf label {
	width:130px;
	float:left;
}

div#registrationf label span {
	color:#c00;
}

div#registrationf input {
	width:200px;
}

div#registrationf textarea {
	width:300px;
	height:150px;
}

div#registrationf input, div#registrationf textarea {
	border:1px #ccc solid;
}

div#registrationf input:hover, div#registrationf textarea:hover {
	border:1px #666 solid;
}

div#registrationf input#register {
	width:auto;
	color:#FFF;
	background-color:#333;
	border:1px #000 solid !important;
	cursor:pointer;
}

div#registrationf input#register:hover {
	color:#333;
	background-color:#fff;
	border:1px #333 solid;
}

/*Inceput - stilurile erorilor*/
div#registrationf p {
	color:#c00;
	padding:0 0 0 130px;
	font-size:10px;
	text-align:left;
}

div#registrationf div#username_field label,
div#registrationf div#email_field label,
div#registrationf div#password_field label,
div#registrationf div#retype_password_field label {
	color:#c00;
}

div#registrationf div#username_field input,
div#registrationf div#email_field input,
div#registrationf div#password_field input,
div#registrationf div#retype_password_field input {
	border:1px #c00 solid;
	color:#c00;
}

div#registrationf div#username_field input:hover,
div#registrationf div#email_field input:hover,
div#registrationf div#password_field input:hover,
div#registrationf div#retype_password_field input:hover {
	border:1px #c00 solid;
}
/*Sfarsit - stilurile erorilor*/

div#registrationf a#back {
	color:#900;
	font-weight:bold;
	text-decoration:underline;
}

/*SFARSIT - LINIILE CSS CARE CREEAZA SKINUL FORMULARULUI DE INREGISTRARE*/
&lt;/style&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;div id=&quot;registrationf&quot;&gt;
	&lt;h1&gt;
		&lt;label&gt;&amp;nbsp;&lt;/label&gt;
		Register an user
	&lt;/h1&gt;

	&lt;?php if (isset($confirmation)) echo '&lt;p&gt;'.$confirmation.'&lt;/p&gt;'; ?&gt;

	&lt;form action=&quot;&lt;?php echo 'http://'.$_SERVER['HTTP_HOST'].$php_self; ?&gt;&quot; method=&quot;post&quot; &lt;?php if (isset($display)) echo $display; ?&gt;&gt;
		&lt;?php echo (isset($errors['username'])) ? '&lt;p&gt;'.$errors['username'].'&lt;/p&gt;&lt;div id=&quot;username_field&quot;&gt;' : '&lt;div&gt;' ; ?&gt;
			&lt;label for=&quot;username&quot;&gt;Username&lt;span&gt;*&lt;/span&gt;:&lt;/label&gt;
			&lt;input name=&quot;username&quot; type=&quot;text&quot; id=&quot;username&quot; value=&quot;&lt;?php if (isset($post['username'])) echo $post['username']; ?&gt;&quot; /&gt;
		&lt;/div&gt;

		&lt;?php echo (isset($errors['email'])) ? '&lt;p&gt;'.$errors['email'].'&lt;/p&gt;&lt;div id=&quot;email_field&quot;&gt;' : '&lt;div&gt;' ; ?&gt;
			&lt;label for=&quot;email&quot;&gt;Email&lt;span&gt;*&lt;/span&gt;:&lt;/label&gt;
			&lt;input name=&quot;email&quot; type=&quot;text&quot; id=&quot;email&quot; value=&quot;&lt;?php if (isset($post['email'])) echo $post['email']; ?&gt;&quot; /&gt;
		&lt;/div&gt;

		&lt;?php echo (isset($errors['password'])) ? '&lt;p&gt;'.$errors['password'].'&lt;/p&gt;&lt;div id=&quot;password_field&quot;&gt;' : '&lt;div&gt;' ; ?&gt;
			&lt;label for=&quot;password&quot;&gt;Password&lt;span&gt;*&lt;/span&gt;:&lt;/label&gt;
			&lt;input name=&quot;password&quot; type=&quot;password&quot; id=&quot;password&quot; /&gt;
		&lt;/div&gt;

		&lt;?php echo (isset($errors['retype_password'])) ? '&lt;p&gt;'.$errors['retype_password'].'&lt;/p&gt;&lt;div id=&quot;retype_password_field&quot;&gt;' : '&lt;div&gt;' ; ?&gt;
			&lt;label for=&quot;retype_password&quot;&gt;Retype password&lt;span&gt;*&lt;/span&gt;:&lt;/label&gt;
			&lt;input name=&quot;retype_password&quot; type=&quot;password&quot; id=&quot;retype_password&quot; /&gt;
		&lt;/div&gt;

		&lt;div&gt;
			&lt;label&gt; &amp;nbsp; &lt;/label&gt;
			&lt;input name=&quot;register&quot; type=&quot;submit&quot; id=&quot;register&quot; value=&quot;register&quot; /&gt;
		&lt;/div&gt;
	&lt;/form&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>Pentru un mai bun managment al codului este indicat ca scriptul PHP de deasupra DOCTYPE-ului, care valideaza datele introduse in formular si inregistreaza utilizatorul in baza de date, sa se bage intr-un fisier include si sa fie apelat prin functia PHP include().</p>
<pre class="brush: php;">
&lt;?php @include('includes/register_user.inc.php'); ?&gt;
</pre>
<p>De asemenea, este indicat ca liniile CSS, care creeaza skinul formularului de inregistrare, din sectiunea &lt;head&gt; &lt;/head&gt; sa fie bagate intr-un fisier CSS extern si apelate cu tagul HTML &lt;link /&gt;.</p>
<pre class="brush: xml;">
&lt;link href=&quot;css/registration_form_design.css&quot; type=&quot;text/css&quot; rel=&quot;stylesheet&quot; media=&quot;all&quot; /&gt;
</pre>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Cum+fac+un+formular+de+inregistrare+a+utilizatorilor%3F+http://nzgae.th8.us" title="Publica acest articol pe Twitter"><img class="nothumb" src="http://www.accesinterzis.ro/wp-content/plugins/tweet-this/icons/tt-twitter.png" alt="Post to Twitter" /></a> <a class="tt" href="http://twitter.com/home/?status=Cum+fac+un+formular+de+inregistrare+a+utilizatorilor%3F+http://nzgae.th8.us" title="Publica acest articol pe Twitter">Publica acest articol pe Twitter</a></p>

<p><strong>Articole asemanatoare:<ul><li><a href='http://www.accesinterzis.ro/index.php/web/cum-fac-un-formular-de-inregistrare-a-utilizatorilor-versiunea-2/' rel='bookmark' title='Permanent Link: Cum fac un formular de inregistrare a utilizatorilor? (versiunea 2)'>Cum fac un formular de inregistrare a utilizatorilor? (versiunea 2)</a></li>
<li><a href='http://www.accesinterzis.ro/index.php/programare/cum-fac-un-formular-de-autentificare-a-utilizatorilor/' rel='bookmark' title='Permanent Link: Cum fac un formular de autentificare a utilizatorilor?'>Cum fac un formular de autentificare a utilizatorilor?</a></li>
<li><a href='http://www.accesinterzis.ro/index.php/programare/cum-fac-un-formular-de-comentarii/' rel='bookmark' title='Permanent Link: Cum fac un formular de comentarii?'>Cum fac un formular de comentarii?</a></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://www.accesinterzis.ro/index.php/programare/cum-fac-un-formular-de-inregistrare-a-utilizatorilor/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Cum fac un formular de comentarii?</title>
		<link>http://www.accesinterzis.ro/index.php/programare/cum-fac-un-formular-de-comentarii/</link>
		<comments>http://www.accesinterzis.ro/index.php/programare/cum-fac-un-formular-de-comentarii/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 21:34:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[programare]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.accesinterzis.ro/?p=144</guid>
		<description><![CDATA[UPDATED ARTICLE
Prin ce se caracterizeaza acest formular de comentarii?

scriptul PHP isi creeaza singur tabelul in care va stoca comentariile daca acesta nu exista in baza de date (deci mai putina munca in phpmyadmin)
protectie antispam 100% datorita sistemului CAPTCHA integrat
toate datele introduse sunt validate server-side (nu bag mana in foc caci inca nu sunt doxa de [...]


<strong>Articole asemanatoare:<ul><li><a href='http://www.accesinterzis.ro/index.php/programare/cum-fac-un-formular-de-comentarii-versiunea-2/' rel='bookmark' title='Permanent Link: Cum fac un formular de comentarii? (versiunea 2)'>Cum fac un formular de comentarii? (versiunea 2)</a></li>
<li><a href='http://www.accesinterzis.ro/index.php/programare/cum-fac-un-formular-de-inregistrare-a-utilizatorilor/' rel='bookmark' title='Permanent Link: Cum fac un formular de inregistrare a utilizatorilor?'>Cum fac un formular de inregistrare a utilizatorilor?</a></li>
<li><a href='http://www.accesinterzis.ro/index.php/programare/cum-fac-un-formular-de-contact/' rel='bookmark' title='Permanent Link: Cum fac un formular de contact?'>Cum fac un formular de contact?</a></li>
</ul>]]></description>
			<content:encoded><![CDATA[<p><span style="color: #ff0000;"><strong>UPDATED ARTICLE</strong></span></p>
<p>Prin ce se caracterizeaza acest formular de comentarii?</p>
<ul>
<li>scriptul PHP isi creeaza singur tabelul in care va stoca comentariile daca acesta nu exista in baza de date (deci mai putina munca in phpmyadmin)</li>
<li>protectie antispam 100% datorita sistemului CAPTCHA integrat</li>
<li>toate datele introduse sunt validate server-side (nu bag mana in foc caci inca nu sunt doxa de programare, dar cred ca este securizat destul ca sa nu execute vreun rau-voitor o injectie sql)</li>
<li>evidentierea campurilor care contin date invalide</li>
<li>pastrarea datelor in campuri atunci cand scriptul detecteaza o eroare</li>
<li>background-ul fiecarui comentariu alterneaza de la un comentariu la altul pentru o mai buna vizualizare a comentariilor</li>
<li>designul formularului este creat EXCLUSIV din CSS</li>
</ul>
<p><span id="more-144"></span></p>
<p>Asadar, ca sa realizez un formular de cometarii mai intai creez in baza de date tabelul care va stoca toate comentariile.</p>
<pre class="brush: php;">
$q = &quot;CREATE TABLE IF NOT EXISTS ai_commentform(
		comment_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
		name VARCHAR(30) NOT NULL,
		email VARCHAR(30) NOT NULL,
		website VARCHAR(30) NOT NULL,
		comment TEXT NOT NULL,
		date DATETIME NOT NULL,
		ip VARCHAR(30) NOT NULL,
		PRIMARY KEY(comment_id))&quot;;

mysql_query($q) or die(mysql_error());
</pre>
<p>Mai avem nevoie de <a title="Cum ma conectez la o baza de date?" href="http://www.accesinterzis.ro/index.php/programare/cum-ma-conectez-la-o-baza-de-date/" target="_blank">connect_to_db.inc.php</a>, fisierul care ne conecteaza la baza de date.</p>
<pre class="brush: php;">
&lt;?php
/*
Titlu: Cum fac un formular de comentarii?
Autor: Marian Barbu aka AccesInterzis
Website: http://www.accesinterzis.ro
2010 (c) Toate drepturile rezervate
*/

//-----creez o sesiune pe server pentru a salva in ea codul generat aleatoriu de sistemul CAPTCHA
#1
session_start();

//-----ma conectez la baza de date
#2
include('includes/connect_to_db.inc.php');

//-----infasor in strip_tags() si htmlentities() URL-urile obtinute dinamic ca sa ma asigur ca nu contin cod malitios
#3
$php_self = htmlentities(strip_tags($_SERVER['PHP_SELF']), ENT_QUOTES, 'utf-8');
$referer = (isset($_SERVER['HTTP_REFERER'])) ? htmlentities(strip_tags($_SERVER['HTTP_REFERER']), ENT_QUOTES, 'utf-8') : NULL;

//-----specific EXACT cu ce campuri se va lucra
#4
$required_fields = array('name', 'email', 'website', 'comment', 'security_code', 'add');
$sent_fields = array_keys($_POST);

//------scriptul PHP se executa doar daca cererea a fost facuta de pe aceeasi pagina pe care se afla formularul si doar daca toate campurile formularului au fos trimise
#5
if ($referer == 'http://'.$_SERVER['HTTP_HOST'].$php_self &amp;&amp; $required_fields == $sent_fields) {
	//-----initializez array-ul in care voi stoca mesajele de eroare si array-ul in care voi pasa datele din $_POST dupa ce le filtrez
	#5.1
	$errors = array();
	$post = array();

	#5.2
	//Starting data validation
	if (empty($_POST['name'])) {
		$errors['name'] = 'You forgot to enter the &lt;strong&gt;name&lt;/strong&gt;.';
	} else {
		$post['name'] = trim($_POST['name']);

		if (ini_get('magic_quotes_gpc')) {
			$post['name'] = stripslashes($post['name']);
		}

		if (strlen($post['name']) &lt; 3) {
			$errors['name'] = 'The &lt;strong&gt;name&lt;/strong&gt; is too short.';
		} else {
			if (strlen($post['name']) &gt; 30) {
				$errors['name'] = 'The &lt;strong&gt;name&lt;/strong&gt; is too long.';
			} else {
				if (!preg_match('/[a-z0-9_ ]*/i', $post['name'])) {
					$errors['name'] = 'The &lt;strong&gt;name&lt;/strong&gt; isn\'t valid.';
				}
			}
		}
	}

	if (empty($_POST['email'])) {
		$errors['email'] = 'You forgot to enter the &lt;strong&gt;email address&lt;/strong&gt;.';
	} else {
		$post['email'] = trim($_POST['email']);

		if (ini_get('magic_quotes_gpc')) {
			$post['email'] = stripslashes($post['email']);
		}

		if (strlen($post['email']) &lt; 3) {
			$errors['email'] = 'The &lt;strong&gt;email address&lt;/strong&gt; is too short.';
		} else {
			if (strlen($post['email']) &gt; 30) {
				$errors['email'] = 'The &lt;strong&gt;email address&lt;/strong&gt; is too long.';
			} else {
				if (!preg_match('/^[a-z0-9][a-z0-9_.]+@[a-z0-9-.]+\.[a-z]{2,4}$/i', $post['email'])) {
					$errors['email'] = 'The &lt;strong&gt;email address&lt;/strong&gt; isn\'t valid.';
				}
			}
		}
	}

	if (empty($_POST['website'])) {
		$post['website'] = NULL;
	} else {
		$post['website'] = trim($_POST['website']);

		if (ini_get('magic_quotes_gpc')) {
			$post['website'] = stripslashes($post['website']);
		}

		if (strlen($post['website']) &lt; 7) {
			$errors['website'] = 'The &lt;strong&gt;website&lt;/strong&gt; URL is too short.';
		} else {
			if (strlen($post['website']) &gt; 30) {
				$errors['website'] = 'The &lt;strong&gt;website URL&lt;/strong&gt; is too long.';
			} else {
				if (!preg_match('/^http:\/\/[a-z0-9.-]+\.[a-z]{2,4}$/i', $post['website'])) {
					$errors['website'] = 'The website URL isn\'t valid.';
				}
			}
		}
	}

	if (empty($_POST['comment'])) {
		$errors['comment'] = 'You forgot to enter the &lt;strong&gt;comment&lt;/strong&gt;.';
	} else {
		$post['comment'] = trim($_POST['comment']);

		if (ini_get('magic_quotes_gpc')) {
			$post['comment'] = stripslashes($post['comment']);
		}

		if (strlen($post['comment']) &lt; 2) {
			$errors['comment'] = 'The &lt;strong&gt;comment&lt;/strong&gt; is too short.';
		} else {
			if (strlen($post['comment']) &gt; 100000) {
				$errors['comment'] = 'The &lt;strong&gt;comment&lt;/strong&gt; is too long.';
			}
		}
	}

	if (empty($_POST['security_code'])) {
		$errors['security_code'] = 'You forgot to enter the &lt;strong&gt;security code&lt;/strong&gt;.';
	} else {
		$post['security_code'] = trim($_POST['security_code']);

		if (ini_get('magic_quotes_gpc')) {
			$post['security_code'] = stripslashes($post['security_code']);
		}

		if (strlen($post['security_code']) &lt; 3) {
			$errors['security_code'] = 'The &lt;strong&gt;security code&lt;/strong&gt; is too short.';
		} else {
			if (strlen($post['security_code']) &gt; 10) {
				$errors['security_code'] = 'The &lt;strong&gt;security code&lt;/strong&gt; is too long.';
			} else {
				if (!preg_match('/^[a-z0-9]*$/i', $post['security_code'])) {
					$errors['security_code'] = 'The &lt;strong&gt;security code&lt;/strong&gt; isn\'t valid.';
				} else {
					if ($_SESSION['security_code'] != $_POST['security_code']) {
						$errors['security_code'] = 'The &lt;strong&gt;security code&lt;/strong&gt; entered is wrong.';
					}
				}
			}
		}
	}
	//Ending data validation

	//------daca nu exista niciun fel de erori bag datele in baza de date
    #5.3
	if(count($errors) == 0) {
		//-----infasor datele in mysql_real_escape_string() deoarece urmeaza sa interoghez baza de date
		#5.3.1
		foreach ($post as $k =&gt; $v) {
			$post[$k] = mysql_real_escape_string($v);
		}

        #5.3.2
		$q = &quot;INSERT INTO ai_commentform(`name`, `email`, `website`, `comment`, `date`, `ip`)&quot;
			.&quot;VALUES('&quot;.$post['name'].&quot;', '&quot;.$post['email'].&quot;', '&quot;.$post['website'].&quot;', '&quot;.$post['comment'].&quot;', NOW(), '&quot;.$_SERVER['REMOTE_ADDR'].&quot;')&quot;;

		#5.3.3
		if (mysql_query($q) == false) {
			$error_message = 'Something is wrong with the server. Your comment wasn\'t added.';
		}
    }

	//-----infasor datele in htmlentities() deoarece urmeaza sa le afisez in formular
	#5.4
	foreach ($post as $k =&gt; $v) {
		$post[$k] = htmlentities(stripslashes($v), ENT_QUOTES, 'utf-8');
	}
}
?&gt;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;How do I make a comment form?&lt;/title&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
&lt;meta name=&quot;description&quot; content=&quot;How do I make a comment form?&quot; /&gt;
&lt;meta name=&quot;keywords&quot; content=&quot;comment,form,php,script,add,email&quot; /&gt;
&lt;meta name=&quot;abstract&quot; content=&quot;How do I make a comment form?&quot; /&gt;
&lt;meta name=&quot;author&quot; content=&quot;AccesInterzis&quot; /&gt;
&lt;meta name=&quot;copyright&quot; content=&quot;AccesInterzis&quot; /&gt;
&lt;meta name=&quot;robots&quot; content=&quot;index,follow&quot; /&gt;
&lt;meta name=&quot;revisit-after&quot; content=&quot;7 days&quot; /&gt;

&lt;style type=&quot;text/css&quot;&gt;
* {
	margin:0;
	padding:0;
	outline:none;
}

html {
	color:black;
	background-color:white;
	font: normal normal normal 12px  Verdana;
	/*font-style font-variant font-weight font-size font-family*/
}

/*INCEPUT - LINIILE CSS CARE CREEAZA SKINUL FORMULARULUI DE CONTACT*/

div#commentf {
	width:530px;
	margin:0px auto;
}

div#commentf h1 {
	color:black;
	font: normal normal normal 24px  Verdana;
	/*font-style font-variant font-weight font-size font-family*/
	padding-bottom:5px;
}

div#commentf div {
	margin:0 0 5px 0;
}

div#commentf label {
	width:115px;
	float:left;
}

div#commentf label span {
	color:#c00;
}

div#commentf input {
	width:200px;
}

div#commentf textarea {
	width:400px;
	height:150px;
}

div#commentf input, div#commentf textarea {
	border:1px #ccc solid;
}

div#commentf input:hover, div#commentf textarea:hover {
	border:1px #666 solid;
}

div#commentf input#add {
	width:auto;
	color:#FFF;
	background-color:#333;
	border:1px #000 solid !important;
	cursor:pointer;
}

div#commentf input#add:hover {
	color:#333;
	background-color:#fff;
	border:1px #333 solid;
}

/*Inceput - stilurile erorilor*/
div#commentf form#form-itself p {
	color:#c00;
	font-size:10px;
	padding:0 0 0 115px;
	text-align:left;
}

div#commentf div#name_field label,
div#commentf div#email_field label,
div#commentf div#website_field label,
div#commentf div#comment_field label,
div#commentf div#security_code_field label {
	color:#c00;
}

div#commentf div#name_field input,
div#commentf div#email_field input,
div#commentf div#website_field input,
div#commentf div#comment_field textarea,
div#commentf div#security_code_field input {
	border:1px #c00 solid;
	color:#c00;
}

div#commentf div#name_field input:hover,
div#commentf div#email_field input:hover,
div#commentf div#website_field input:hover,
div#commentf div#comment_field textarea:hover,
div#commentf div#security_code_field input:hover {
	border:1px #c00 solid;
}
/*Sfarsit - stilurile erorilor*/

div#comments h1 {
	color:#c00;
	font: normal normal normal 24px  Verdana;
	/*font-style font-variant font-weight font-size font-family*/
	padding-bottom:5px;
}

/*SFARSIT - LINIILE CSS CARE CREEAZA SKINUL FORMULARULUI DE CONTACT*/
&lt;/style&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;?php
$q = &quot;SELECT name, website, date, comment FROM ai_commentform ORDER BY comment_id DESC&quot;;
$result = mysql_query($q) or die(mysql_error());

/*
Cand citesc datele din baza de date le infasor in stripslashes(), htmlentities() si nl2br().
*/
if (mysql_num_rows($result) != 0) {
	echo '&lt;div id=&quot;comments&quot;&gt;';
	echo '&lt;h1&gt;'.mysql_num_rows($result).' comment(s) so far&lt;/h1&gt;';

	$i = 0;
	while ($row = mysql_fetch_array($result)) {
		echo '&lt;p&gt;';
		echo ($row['website'] == '') ? '&lt;strong&gt;'.$row['name'].'&lt;/strong&gt;' : '&lt;a href=&quot;'.$row['website'].'&quot; title=&quot;'.$row['website'].'&quot; target=&quot;_blank&quot;&gt;'.$row['name'].'&lt;/a&gt;';
		echo ' - '.$row['date'];
		echo '&lt;/p&gt;';

		echo (is_int($i/2)) ? '&lt;p style=&quot;margin-bottom:15px;&quot;&gt;' : '&lt;p style=&quot;margin-bottom:15px; background:#ccc;&quot;&gt;';
		echo nl2br(htmlentities(stripslashes($row['comment']), ENT_QUOTES, 'utf-8'));
		echo '&lt;/p&gt;';

		$i++;
	}
	echo '&lt;/div&gt;';
}
?&gt;
&lt;div id=&quot;commentf&quot;&gt;
	&lt;h1&gt;
		&lt;label&gt;&amp;nbsp;&lt;/label&gt;
		Leave a comment
	&lt;/h1&gt;

	&lt;form action=&quot;&lt;?php echo 'http://'.$_SERVER['HTTP_HOST'].$php_self; ?&gt;&quot; method=&quot;post&quot; id=&quot;form-itself&quot;&gt;
		&lt;?php echo (isset($errors['name'])) ? '&lt;p&gt;'.$errors['name'].'&lt;/p&gt;&lt;div id=&quot;name_field&quot;&gt;' : '&lt;div&gt;' ; ?&gt;
			&lt;label for=&quot;name&quot;&gt;Name&lt;span&gt;*&lt;/span&gt;:&lt;/label&gt;
			&lt;input name=&quot;name&quot; type=&quot;text&quot; id=&quot;name&quot; value=&quot;&lt;?php if (isset($post['name'])) echo $post['name']; ?&gt;&quot; /&gt;
		&lt;/div&gt;

		&lt;?php echo (isset($errors['email'])) ? '&lt;p&gt;'.$errors['email'].'&lt;/p&gt;&lt;div id=&quot;email_field&quot;&gt;' : '&lt;div&gt;' ; ?&gt;
			&lt;label for=&quot;email&quot;&gt;Email&lt;span&gt;*&lt;/span&gt;:&lt;/label&gt;
			&lt;input name=&quot;email&quot; type=&quot;text&quot; id=&quot;email&quot; value=&quot;&lt;?php if (isset($post['email'])) echo $post['email']; ?&gt;&quot; /&gt;
		&lt;/div&gt;

		&lt;?php echo (isset($errors['website'])) ? '&lt;p&gt;'.$errors['website'].'&lt;/p&gt;&lt;div id=&quot;website_field&quot;&gt;' : '&lt;div&gt;' ; ?&gt;
			&lt;label for=&quot;website&quot;&gt;Website:&lt;/label&gt;
			&lt;input name=&quot;website&quot; type=&quot;text&quot; id=&quot;website&quot; value=&quot;&lt;?php if (isset($post['website'])) echo $post['website']; ?&gt;&quot; /&gt;
		&lt;/div&gt;

		&lt;?php echo (isset($errors['comment'])) ? '&lt;p&gt;'.$errors['comment'].'&lt;/p&gt;&lt;div id=&quot;comment_field&quot;&gt;' : '&lt;div&gt;' ; ?&gt;
			&lt;label for=&quot;comment&quot;&gt;Your comment&lt;span&gt;*&lt;/span&gt;:&lt;/label&gt;
			&lt;textarea name=&quot;comment&quot; rows=&quot;1&quot; cols=&quot;1&quot; id=&quot;comment&quot;&gt;&lt;?php if (isset($post['comment'])) echo $post['comment']; ?&gt;&lt;/textarea&gt;
		&lt;/div&gt;

		&lt;p&gt;
			&lt;img src=&quot;includes/captchaimage.inc.php?width=120&amp;height=40&amp;characters=5&quot; /&gt;
		&lt;p&gt;

		&lt;?php echo (isset($errors['security_code'])) ? '&lt;p&gt;'.$errors['security_code'].'&lt;/p&gt;&lt;div id=&quot;security_code_field&quot;&gt;' : '&lt;div&gt;' ; ?&gt;
			&lt;label for=&quot;security_code&quot;&gt;Are you human?&lt;span&gt;*&lt;/span&gt;&lt;/label&gt;
			&lt;input id=&quot;security_code&quot; name=&quot;security_code&quot; type=&quot;text&quot; /&gt;
		&lt;/div&gt;

		&lt;div&gt;
			&lt;label&gt; &amp;nbsp; &lt;/label&gt;
			&lt;input name=&quot;add&quot; type=&quot;submit&quot; id=&quot;add&quot; value=&quot;add&quot; /&gt;
		&lt;/div&gt;
	&lt;/form&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>Felul in care se integreaza sistemul CAPTCHA in formular il puteti gasi in <a title="Cum fac un formular de contact?" href="http://www.accesinterzis.ro/index.php/programare/cum-fac-un-formular-de-contact/" target="_blank">articolul</a> care prezinta cum se realizeaza un formular de contact.</p>
<p>Pentru un mai bun managment al codului este indicat ca scriptul PHP de deasupra DOCTYPE-ului, care valideaza datele introduse in formular si introduce comentariile in baza de date, sa se bage intr-un fisier include si sa fie apelat prin functia PHP include().</p>
<pre class="brush: php;">
&lt;?php @include('includes/add_comment.inc.php'); ?&gt;
</pre>
<p>De asemenea, este indicat ca liniile CSS, care creeaza skinul formularului de comentarii, din sectiunea &lt;head&gt; &lt;/head&gt; sa fie bagate intr-un fisier CSS extern si apelate cu tagul HTML &lt;link /&gt;.</p>
<pre class="brush: xml;">
&lt;link href=&quot;css/comment_form_design.css&quot; type=&quot;text/css&quot; rel=&quot;stylesheet&quot; media=&quot;all&quot; /&gt;
</pre>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Cum+fac+un+formular+de+comentarii%3F+http://5r28a.th8.us" title="Publica acest articol pe Twitter"><img class="nothumb" src="http://www.accesinterzis.ro/wp-content/plugins/tweet-this/icons/tt-twitter.png" alt="Post to Twitter" /></a> <a class="tt" href="http://twitter.com/home/?status=Cum+fac+un+formular+de+comentarii%3F+http://5r28a.th8.us" title="Publica acest articol pe Twitter">Publica acest articol pe Twitter</a></p>

<p><strong>Articole asemanatoare:<ul><li><a href='http://www.accesinterzis.ro/index.php/programare/cum-fac-un-formular-de-comentarii-versiunea-2/' rel='bookmark' title='Permanent Link: Cum fac un formular de comentarii? (versiunea 2)'>Cum fac un formular de comentarii? (versiunea 2)</a></li>
<li><a href='http://www.accesinterzis.ro/index.php/programare/cum-fac-un-formular-de-inregistrare-a-utilizatorilor/' rel='bookmark' title='Permanent Link: Cum fac un formular de inregistrare a utilizatorilor?'>Cum fac un formular de inregistrare a utilizatorilor?</a></li>
<li><a href='http://www.accesinterzis.ro/index.php/programare/cum-fac-un-formular-de-contact/' rel='bookmark' title='Permanent Link: Cum fac un formular de contact?'>Cum fac un formular de contact?</a></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://www.accesinterzis.ro/index.php/programare/cum-fac-un-formular-de-comentarii/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Cum ma conectez la o baza de date?</title>
		<link>http://www.accesinterzis.ro/index.php/programare/cum-ma-conectez-la-o-baza-de-date/</link>
		<comments>http://www.accesinterzis.ro/index.php/programare/cum-ma-conectez-la-o-baza-de-date/#comments</comments>
		<pubDate>Mon, 22 Feb 2010 17:32:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[programare]]></category>
		<category><![CDATA[cod-sursa]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.accesinterzis.ro/?p=4</guid>
		<description><![CDATA[Scriptul PHP de mai jos iti permite sa te conectezi la o baza de date MySQL.

&#60;?php
define('SQL_HOST', 'localhost');
define('SQL_USER', 'username');
define('SQL_PASS', 'password');
define('SQL_DB', 'database name');

@mysql_connect(SQL_HOST, SQL_USER, SQL_PASS) or die('Nu ma pot conecta la serverul MySQL!');
@mysql_select_db(SQL_DB) or die('Nu ma pot conecta la baza de date!');
?&#62;

De obicei, acest script este un fisier include care este apelat, de cele mai multe ori [...]


<strong>Niciun articol asemanator.</strong>]]></description>
			<content:encoded><![CDATA[<p>Scriptul PHP de mai jos iti permite sa te conectezi la o baza de date MySQL.</p>
<pre class="brush: php;">
&lt;?php
define('SQL_HOST', 'localhost');
define('SQL_USER', 'username');
define('SQL_PASS', 'password');
define('SQL_DB', 'database name');

@mysql_connect(SQL_HOST, SQL_USER, SQL_PASS) or die('Nu ma pot conecta la serverul MySQL!');
@mysql_select_db(SQL_DB) or die('Nu ma pot conecta la baza de date!');
?&gt;
</pre>
<p>De obicei, acest script este un fisier include care este apelat, de cele mai multe ori deasupra DOCTYPE-ului, de fiecare data cand o pagina web trebuie sa se coneteze la o baza de date.</p>
<pre class="brush: php;">
&lt;?php include(&quot;includes/connect_to_db.inc.php&quot;); ?&gt;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
</pre>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Cum+ma+conectez+la+o+baza+de+date%3F+http://kd4ca.th8.us" title="Publica acest articol pe Twitter"><img class="nothumb" src="http://www.accesinterzis.ro/wp-content/plugins/tweet-this/icons/tt-twitter.png" alt="Post to Twitter" /></a> <a class="tt" href="http://twitter.com/home/?status=Cum+ma+conectez+la+o+baza+de+date%3F+http://kd4ca.th8.us" title="Publica acest articol pe Twitter">Publica acest articol pe Twitter</a></p>

<p><strong>Niciun articol asemanator.</strong></p>]]></content:encoded>
			<wfw:commentRss>http://www.accesinterzis.ro/index.php/programare/cum-ma-conectez-la-o-baza-de-date/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

