<?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>Ahsun Taquveem Chohan &#187; php &amp; mysql</title>
	<atom:link href="http://ahsun.xenoglaux-solutions.com/category/php_mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://ahsun.xenoglaux-solutions.com</link>
	<description>The Blog for computer scientists</description>
	<lastBuildDate>Sat, 04 Dec 2010 08:47:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>Creating A File Upload Script</title>
		<link>http://ahsun.xenoglaux-solutions.com/2009/04/28/creating-a-file-upload-script/</link>
		<comments>http://ahsun.xenoglaux-solutions.com/2009/04/28/creating-a-file-upload-script/#comments</comments>
		<pubDate>Tue, 28 Apr 2009 18:31:12 +0000</pubDate>
		<dc:creator>Ahsun Taquveem Chohan</dc:creator>
				<category><![CDATA[php & mysql]]></category>
		<category><![CDATA[bmp]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[create]]></category>
		<category><![CDATA[domain]]></category>
		<category><![CDATA[extension]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[jpg]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[path]]></category>
		<category><![CDATA[permission]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[upload]]></category>

		<guid isPermaLink="false">http://ahsun.xenoglaux-solutions.com/2009/04/28/creating-a-file-upload-script/</guid>
		<description><![CDATA[I will teach you how to create a basic script that allow people to upload files and stores details of their uploads in a database. Follow up, it is not hard at all&#8230;. If you have questions, feel free to ask First we need to create the database table with the following SQL: &#160; CREATE [...]]]></description>
			<content:encoded><![CDATA[<p>I will teach you how to create a basic script that allow people to upload files and stores details of their uploads in a database. Follow up, it is not hard at all&#8230;. If you have questions, feel free to ask</p>
<p>First we need to create the database table with the following SQL:</p>
<p>&#160;</p>
<pre class="prettyprint">CREATE TABLE `uploads` (
`id` int(10) unsigned NOT NULL auto_increment,
`whenuploaded` datetime NOT NULL default '0000-00-00 00:00:00',
`ipaddress` varchar(15) NOT NULL default 'unknown',
`imageloc` varchar(255) NOT NULL default 'unknown',
`imagesize` int(10) unsigned default NULL,
`imagetype` varchar(30) default NULL,
PRIMARY KEY (`id`)
) TYPE=MyISAM;</pre>
</p>
<p>&#160;</p>
<p>Then here&#8217;s the PHP code you need at the top of the page where you&#8217;re going to have your upload form. You need to change the database access details at the top and the &#8216;defines&#8217; to specify your domain and path names. Create the DEST_DIR directory on the server with 777 permissions.</p>
<p><span id="more-54"></span></p>
<pre class="prettyprint">define('MAX_ALLOWED_FILE_SIZE', 1024000); // change 1024000 with the size you wish to allow people to upload
define(&quot;DEST_DIR&quot;, '/upload/'); / / set your upload dir
define('DEST_PATH', '/home/public_html' . DEST_DIR); //path to your dir
define('DEST_URL', 'http://yourwebsite.com' . DEST_DIR);

$allowed_types = array(&quot;image/gif&quot;, &quot;image/pjpeg&quot;, &quot;image/x-png&quot;, &quot;image/bmp&quot;); //set the allowed extensions

$dbhost = &quot;localhost&quot;;
$dbname = &quot;yourdb_name&quot;;
$dbuser = &quot;yourdb_user&quot;;
$dbpass = &quot;yourdb_password&quot;;

$errormessage = &quot;Please enter file to be uploaded.&quot;; //change this message to whatever U want

if ((isset($_REQUEST['form_submit'])) &amp;&amp; ('form_uploader' == $_REQUEST['form_submit']))
{
$picfile_name = $_FILES['picfile']['name'];
$picfile_type = $_FILES['picfile']['type'];
$picfile_size = $_FILES['picfile']['size'];
$picfile_temp = $_FILES['picfile']['tmp_name'];

if (MAX_ALLOWED_FILE_SIZE &gt;= $picfile_size)
{
if (in_array($picfile_type, $allowed_types))
{
if (is_uploaded_file($_FILES['picfile']['tmp_name']))
{

if (file_exists(DEST_PATH . $picfile_name))
{
$unique_id = time();
$picfile_name = $unique_id . '_' . $picfile_name;
}

if (move_uploaded_file($picfile_temp, DEST_PATH . $picfile_name))
{
$errormessage = &quot;File uploaded as:
&quot; . DEST_URL . $picfile_name . &quot;&quot;;

if(mysql_connect($dbhost, $dbuser, $dbpass))
{
if(mysql_select_db($dbname))
{
$sql1 = &quot;INSERT INTO uploads (whenuploaded, ipaddress, imageloc, imagesize, imagetype) VALUES (&quot;;
$sql1 .= &quot;'&quot; . date(&quot;Y-m-d H:i:s&quot;) . &quot;',&quot;;
$sql1 .= &quot;'&quot; . $_SERVER['REMOTE_ADDR'] . &quot;',&quot;;
$sql1 .= &quot;'&quot; . DEST_DIR . $picfile_name . &quot;',&quot;;
$sql1 .= &quot;&quot; . $picfile_size . &quot;,&quot;;
$sql1 .= &quot;'&quot; . $picfile_type . &quot;')&quot;;

if (!mysql_query($sql1))
{
$errormessage .= &quot;
Query failed [$sql1].&quot;;
}
}
else
{
$errormessage .= &quot;
Could not select database.&quot;;
}
}
else
{
$errormessage .= &quot;
Could not connect to database.&quot;;
}
}
else
{
$errormessage = &quot;File upload failed for obscure reasons (error code: &quot; . $_FILES['picfile']['error'] . &quot;).&quot;;
}
}
else
{
$errormessage = &quot;No file uploaded.&quot;;
}
}
else
{
$errormessage = &quot;Invalid file type.&quot;;
}
}
else
{
$errormessage = &quot;File too big (maximum size is &quot; . MAX_ALLOWED_FILE_SIZE . &quot;).&quot;;
}
$_REQUEST['form_submit'] = &quot;&quot;;
}

?&gt;</pre>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://ahsun.xenoglaux-solutions.com/2009/04/28/creating-a-file-upload-script/" target="_blank"><img src="http://ahsun.xenoglaux-solutions.com/wp-content/plugins/add-to-facebook-plugin/facebook_share_icon.gif" alt="Share on Facebook" title="Share on Facebook" /></a><a href="http://www.facebook.com/share.php?u=http://ahsun.xenoglaux-solutions.com/2009/04/28/creating-a-file-upload-script/" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://ahsun.xenoglaux-solutions.com/2009/04/28/creating-a-file-upload-script/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Login &amp; Logout tutorial</title>
		<link>http://ahsun.xenoglaux-solutions.com/2009/04/01/login-logout-tutorial/</link>
		<comments>http://ahsun.xenoglaux-solutions.com/2009/04/01/login-logout-tutorial/#comments</comments>
		<pubDate>Wed, 01 Apr 2009 17:46:28 +0000</pubDate>
		<dc:creator>Ahsun Taquveem Chohan</dc:creator>
				<category><![CDATA[php & mysql]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[free]]></category>
		<category><![CDATA[login]]></category>
		<category><![CDATA[logout]]></category>
		<category><![CDATA[md5]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://ahsun.xenoglaux-solutions.com/?p=7</guid>
		<description><![CDATA[Create a file for Login and Logout (PHP + MySQL) using with a SESSION variable. This file contains Login form, Login authorize program and Logout program. All in one but Short and Easily. This tutorial require 2 PHP files and 1 table of mySQL database. index.php this file is the Login and Logout file. main.php [...]]]></description>
			<content:encoded><![CDATA[<div>Create a file for Login and Logout (PHP + MySQL) using with a SESSION variable. This file contains Login form, Login authorize program and Logout program. All in one but Short and Easily.</div>
<p>This tutorial require 2 PHP files and 1 table of mySQL database.</p>
<ol>
<li><em>index.php</em> this file is the Login and Logout file.</li>
<li><em>main.php</em> this file is the target when login is O.K.</li>
<li>Database &#8220;tutorial&#8221; and table &#8220;admin&#8221; with 3 fields: id(auto_increment), username(varchar, 50), password(varchar, 32).<br />
* The &#8220;password&#8221; field is design for <em>md5()</em> password for more sucurity.Then put a record into this table. In this tutorial, put a record with name &#8220;<span class="orange">admin</span>&#8221; and password &#8220;<span class="orange">81dc9bdb52d04dc20036dbd8313ed055</span>&#8220;. This password was encrypted by <em>md5()</em> function from the real password &#8220;<span class="orange">1234</span>&#8220;.</p>
<p>* When you test this script you must to <span style="text-decoration: underline;">put the real password</span> (1234) in &#8220;password&#8221; box.</li>
</ol>
<h2><span class="header1">•</span> index.php</h2>
<p>The mainly file for do 3 things.</p>
<ul>
<li><strong>Login form.</strong> Including 2 fields &#8220;username&#8221; and &#8220;password&#8221; and submit button &#8220;Login&#8221;.</li>
<li><strong>Login authorize program.</strong> Do authorize check after you submit form, if passed in username and password, this page will re-direct to <em>main.php</em>. If not, show the invalid user or password message.</li>
<li><strong>Logout.</strong> Clear the login session when come back or refresh this page.</li>
</ul>
<p><span id="more-7"></span></p>
<blockquote>
<p class="orange">&lt;?<br />
<span class="comment">// Use session variable on this page. This function must put on the top of page.</span><br />
session_start();</p>
<p class="orange"><span class="comment">////// Logout Section. Delete all session variable.</span><br />
session_destroy();
</p>
<p class="orange">$message=&#8221;";</p>
<p class="orange"><span class="comment">////// Login Section.</span><br />
$Login=$_POST['Login'];<br />
if($Login){ <span class="comment">// If clicked on Login button.</span><br />
$username=$_POST['username'];<br />
$md5_password=md5($_POST['password']); <span class="comment">// Encrypt password with md5() function. </span></p>
<p class="orange"><span class="comment">// Connect database. </span><br />
$host=&#8221;localhost&#8221;; <span class="comment">// Host name.</span><br />
$db_user=&#8221;"; <span class="comment">// MySQL username.</span><br />
$db_password=&#8221;"; <span class="comment">// MySQL password.</span><br />
$database=&#8221;tutorial&#8221;; <span class="comment">// Database name.</span><br />
mysql_connect($host,$db_user,$db_password);<br />
mysql_select_db($database);</p>
<p class="orange"><span class="comment">// Check matching of username and password.</span><br />
$result=mysql_query(&#8220;select * from admin where username=&#8217;$username&#8217; and password=&#8217;$md5_password&#8217;&#8221;);<br />
if(mysql_num_rows($result)!=&#8217;0&#8242;){ <span class="comment">// If match.</span><br />
session_register(&#8220;username&#8221;); <span class="comment">// Craete session username.</span><br />
header(&#8220;location:main.php&#8221;); <span class="comment">// Re-direct to main.php</span><br />
exit;<br />
}else{ <span class="comment">// If not match.</span><br />
$message=&#8221;&#8212; Incorrect Username or Password &#8212;&#8221;;<br />
}</p>
<p class="orange">} <span class="comment">// End Login authorize check.</span><br />
?&gt;</p>
<p>&lt;html xmlns=&#8221;http://www.w3.org/1999/xhtml&#8221;&gt;<br />
&lt;head&gt;<br />
&lt;meta http-equiv=&#8221;Content-Type&#8221; content=&#8221;text/html; charset=utf-8&#8243; /&gt;<br />
&lt;title&gt;Untitled Document&lt;/title&gt;<br />
&lt;/head&gt;</p>
<p>&lt;body&gt;<br />
<span class="orange">&lt;? echo $message; ?&gt;</span><br />
&lt;form id=&#8221;form1&#8243; name=&#8221;form1&#8243; method=&#8221;post&#8221; action=&#8221;<span class="orange">&lt;? echo $PHP_SELF; ?&gt;</span>&#8220;&gt;<br />
&lt;table&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt;User : &lt;/td&gt;<br />
&lt;td&gt;&lt;input name=&#8221;username&#8221; type=&#8221;text&#8221; id=&#8221;username&#8221; /&gt;&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt;Password : &lt;/td&gt;<br />
&lt;td&gt;&lt;input name=&#8221;password&#8221; type=&#8221;password&#8221; id=&#8221;password&#8221; /&gt;&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;/table&gt;<br />
&lt;input name=&#8221;Login&#8221; type=&#8221;submit&#8221; id=&#8221;Login&#8221; value=&#8221;Login&#8221; /&gt;<br />
&lt;/form&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;</p>
</blockquote>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<h2>main.php</h2>
<p>This file is the target file when authorize check on <em>index.php</em> has been passed. It checks for session variable name &#8220;username&#8221;. If this variable does not exist, re-direct to <em>index.php</em>.</p>
<p>If you have PHP files must Login before open them, copy the PHP section and place it on the top of them.</p>
<blockquote><p><span class="orange">&lt;?<br />
</span><span class="comment">// You may copy this PHP section to the top of file which needs to access after login.</span><span class="orange"><br />
session_start(); </span><span class="comment">// Use session variable on this page. This function must put on the top of page.</span><span class="orange"><br />
if(!session_is_registered(&#8220;username&#8221;)){ </span><span class="comment">// if session variable &#8220;username&#8221; does not exist.</span><span class="orange"><br />
header(&#8220;location:index.php&#8221;); </span><span class="comment">// Re-direct to index.php</span><span class="orange"><br />
}<br />
?&gt;</span></p>
<p>&lt;html xmlns=&#8221;http://www.w3.org/1999/xhtml&#8221;&gt;<br />
&lt;head&gt;<br />
&lt;meta http-equiv=&#8221;Content-Type&#8221; content=&#8221;text/html; charset=utf-8&#8243; /&gt;<br />
&lt;title&gt;Untitled Document&lt;/title&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;p&gt;Hello <span class="orange">&lt;? echo $_SESSION['username']; ?&gt;</span>! You are now Logged in.&lt;/p&gt;<br />
&lt;p&gt;&lt;a href=&#8221;index.php&#8221;&gt;Logout&lt;/a&gt;&lt;/p&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;</p></blockquote>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
Now just run it and enjoy <img src='http://ahsun.xenoglaux-solutions.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://ahsun.xenoglaux-solutions.com/2009/04/01/login-logout-tutorial/" target="_blank"><img src="http://ahsun.xenoglaux-solutions.com/wp-content/plugins/add-to-facebook-plugin/facebook_share_icon.gif" alt="Share on Facebook" title="Share on Facebook" /></a><a href="http://www.facebook.com/share.php?u=http://ahsun.xenoglaux-solutions.com/2009/04/01/login-logout-tutorial/" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://ahsun.xenoglaux-solutions.com/2009/04/01/login-logout-tutorial/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

