<?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>testx &#187; ajax</title>
	<atom:link href="http://www.testx.ch/tag/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.testx.ch</link>
	<description></description>
	<lastBuildDate>Wed, 02 Nov 2011 17:54:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>PHP Login mit AJAX</title>
		<link>http://www.testx.ch/2010/01/php-login-mit-ajax/</link>
		<comments>http://www.testx.ch/2010/01/php-login-mit-ajax/#comments</comments>
		<pubDate>Sun, 03 Jan 2010 13:26:43 +0000</pubDate>
		<dc:creator>Fredi</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[php login]]></category>
		<category><![CDATA[php tutorial]]></category>

		<guid isPermaLink="false">http://www.testx.ch/?p=760</guid>
		<description><![CDATA[Ich habe bereits in diesem Artikel erklärt, wie man ein PHP Login mit einer MySQL Datenbank erstellt. Nun möchte ich in diesem Artikel erklären, wie man ein PHP Login mit einer schöneren Benutzeroberfläche programmiert. Der Vorteil besteht darin, dass die Seite nicht neu geladen wird, wenn man sich einloggt. Zusätzlich werden wir das Passwort mit [...]]]></description>
			<content:encoded><![CDATA[<p>Ich habe bereits in <a href="http://www.testx.ch/2009/05/einfaches-php-login/">diesem</a> Artikel erklärt, wie man ein PHP Login mit einer MySQL Datenbank erstellt. Nun möchte ich in diesem Artikel erklären, wie man ein PHP Login mit einer schöneren Benutzeroberfläche programmiert. Der Vorteil besteht darin, dass die Seite nicht neu geladen wird, wenn man sich einloggt. Zusätzlich werden wir das Passwort mit md5 verschlüsseln, um es nicht einfach so lesbar zu machen.</p>
<p>Für dieses Tutorial hier, brauche ich viele Dinge aus dem, welches ich oben verlinkt habe. Deshalb ist es von Vorteil, wenn man sich das Tutorial bereits durchgelesen hat, bevor man mit diesem hier beginnt.</p>
<p>Als erstes solltet ihr alle Dateien aus dem anderen Tutorial erstellen und testen, ob das ganze so funktioniert. Wenn ihr das getan habt, beginnen wir mit der Verschlüsselung der Daten. Man muss sich das so vorstellen, dass beim Eintragen in der Datenbank nicht das Passwort in Klartext abgelegt wird, sondern nur der Hashwert. Wenn sich nun jemand einloggen will, wird das Passwort zuerst auch zu einem md5 Hash verarbeitet und diese beiden Werte dann verglichen.</p>
<p>Dazu müssen wir in der Login.php folgendes verändern:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>?php
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'connect.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$username</span><span style="color: #339933;">=</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$password</span><span style="color: #339933;">=</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$query</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT username, password FROM users WHERE username ='&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$username</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;konnte nicht selecten!!!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Username oder Passwort falsch!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span> <span style="color: #339933;">!=</span> <span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Username oder Passwort falsch!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #990000;">session_register</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Du bist erfolgreich registriert!&quot;</span><span style="color: #339933;">;</span>
?<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span></pre></div></div>

<p>Dadurch wird das empfangene Passwort direkt als Hash in der Variable gespeichert. Das dies funktionieren kann, muss das Passwort auch als md5 Hash in der Datenbank vorhanden sein. Da ich hier keine Registrierungsfunktion einbaue, muss man aus dem Passwort manuell den md5 Hash generieren. Im Web gibt es zahlreiche Websiten, die das anbieten. Falls man so etwas selber machen will, reicht dieser kleine PHP Code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>?php <span style="color: #b1b100;">echo</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Dein Passwort'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> ?<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span></pre></div></div>

<p>Als nächstes müssen wir die loginform.php anpassen. Zuerst benötigen wir aber <a href="http://jquery.com/">JQuery</a>. Wenn wir es heruntergeladen haben, verschieben wir die Datei ins gleiche Verzeichniss wie die anderen Dateien und verändern die loginform.php folgendermassen:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;!</span>DOCTYPE HTML <span style="color: #000000; font-weight: bold;">PUBLIC</span> <span style="color: #0000ff;">&quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;</span><span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>
<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>html<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>
<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>head<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>
<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>title<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>Einfaches Loginscript <span style="color: #339933;">-</span>Tutorial<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;/</span>title<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>
<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>script type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/javascript&quot;</span> src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;jquery-1.3.2.min.js&quot;</span><span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;&amp;</span>lt<span style="color: #339933;">;/</span>script<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>
<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>script type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/javascript&quot;</span><span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>ready<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#submit&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>click<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
$<span style="color: #339933;">.</span>post<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;login.php&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>username<span style="color: #339933;">:</span> $<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#username&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>val<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> password<span style="color: #339933;">:</span> $<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#password&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>val<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#userdata&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>remove<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#result&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>append<span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;/</span>script<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>
<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;/</span>head<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>
<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>body<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>
<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;result&quot;</span><span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>
<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;/</span>div<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>
<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;userdata&quot;</span><span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>
Deine Benutzerdaten<span style="color: #339933;">:&amp;</span>lt<span style="color: #339933;">;</span>br<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>
Benutzername<span style="color: #339933;">:</span>
<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text&quot;</span> size<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;35&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;username&quot;</span><span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;&amp;</span>lt<span style="color: #339933;">;</span>br<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>
Passwort<span style="color: #339933;">:</span>
<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;password&quot;</span> size<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;35&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;&amp;</span>lt<span style="color: #339933;">;</span>br<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>
<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Login&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span><span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>
<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;/</span>div<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>
<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;/</span>body<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>
<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;/</span>html<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span></pre></div></div>

<p>Nun sollte man alle Dateien aus dem anderen Tutorial erstellt haben, die Dateien wie hier beschrieben anpassen  und die MySQL Einträge und Konfiguration vornehmen. Wenn das alles getan ist, kann man die Daten auf einen Webserver hochladen und das ganze Testen. Wenn ihr auf Login klickt, sollte die Loginmaske verschwinden und die Erfolgs-, oder Fehlermeldung erscheinen.</p>
<p>Falls ihr einen Fehler entdeckt oder Fragen habt, schreibt doch einfach einen Kommentar.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.testx.ch/2010/01/php-login-mit-ajax/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Content Delivery Network via Amazon Web Services: CloudFront: dy8cq650okdxd.cloudfront.net

Served from: www.testx.ch @ 2012-02-04 22:23:39 -->
