<?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>XEWeb &#187; MySQL</title>
	<atom:link href="http://www.xeweb.net/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.xeweb.net</link>
	<description>XEWeb blog, PHP scripts and more...</description>
	<lastBuildDate>Mon, 22 Mar 2010 16:32:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How to setup one time download of your files securely</title>
		<link>http://www.xeweb.net/2010/01/03/how-to-setup-one-time-download-of-your-files-securely/</link>
		<comments>http://www.xeweb.net/2010/01/03/how-to-setup-one-time-download-of-your-files-securely/#comments</comments>
		<pubDate>Sun, 03 Jan 2010 13:11:00 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.xeweb.net/?p=102</guid>
		<description><![CDATA[If you are selling a digital product and want to offer it for download but are concerned that people will share the URL to the file, you could use a solution like ClickBank. However the advantage of setting up one time downloads from your server instead is that you have more control over what you [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.xeweb.net/wp-content/uploads/file-download-150x150.gif" alt="How to setup one time download of your files securely" width="150" height="150" class="alignleft size-thumbnail wp-image-105" />If you are selling a digital product and want to offer it for download but are concerned that people will share the URL to the file, you could use a solution like <a href="http://www.clickbank.com">ClickBank</a>. However the advantage of setting up one time downloads from your server instead is that you have more control over what you are selling and you don&#8217;t have to pay any fee&#8217;s.</p>
<p>Let us assume that you have setup and integrated a payment solution (such as PayPal using the IPN, post coming soon on this) and you have a database setup that holds the payment information.</p>
<div class="mysql geshi no mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> <span style="color: #008000;">`transactions`</span> <span style="color: #FF00FF;">&#40;</span><br />
<span style="color: #008000;">`id`</span> <span style="color: #999900; font-weight: bold;">INT</span><span style="color: #FF00FF;">&#40;</span> <span style="color: #008080;">5</span> <span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #FF9900; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #990099; font-weight: bold;">PRIMARY KEY</span> <span style="color: #000033;">,</span><br />
<span style="color: #008000;">`transaction<span style="color: #008080; font-weight: bold;">_</span>id`</span> <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span> <span style="color: #008080;">50</span> <span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #000033;">,</span><br />
<span style="color: #008000;">`file<span style="color: #008080; font-weight: bold;">_</span>downloaded`</span> <span style="color: #999900; font-weight: bold;">TINYINT</span><span style="color: #FF00FF;">&#40;</span> <span style="color: #008080;">1</span> <span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><br />
<span style="color: #FF00FF;">&#41;</span> <span style="color: #000033;">;</span></div>
<p>Now we will create download.php which will be the link sent to the user after they have paid to download your files.</p>
<p>To keep it secure you would include the transaction ID (which would be unique to each payment) as part of the query string, so the link would be:</p>
<p>http://www.site.com/download.php?transaction_id=XX</p>
<p>Now you do need the files to be uploaded somewhere on your web server, however the location will never be revealed. The best way is to put them in a folder outside of the web root so that they cant be accessed via HTTP. However if this is not possible then put them in a folder name that cannot be guessed (use a random password generator if you don&#8217;t have any ideas).</p>
<div class="php geshi no php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #666666; font-style: italic;">// Path to the files to be downloaded</span><br />
<span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&#39;/home/usr/files/blah.zip&#39;</span><span style="color: #339933;">;</span></p>
<p><span style="color: #666666; font-style: italic;">// Transaction ID</span><br />
<span style="color: #000088;">$transaction_id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&#39;transaction_id&#39;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></p>
<p><span style="color: #666666; font-style: italic;">// Connect to database</span><br />
<span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&#39;localhost&#39;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&#39;user&#39;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&#39;pass&#39;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&#39;dbname&#39;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></p>
<p><span style="color: #666666; font-style: italic;">// Lookup the transaction ID in the database</span><br />
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM transactions WHERE transaction_id=&#39;<span style="color: #009933; font-weight: bold;">%s</span>&#39;&quot;</span><span style="color: #339933;">,</span><br />
<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$transaction_id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<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: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></p>
<p><span style="color: #666666; font-style: italic;">// Valid transaction ID?</span><br />
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&#39;Invalid transaction ID!&#39;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></p>
<p><span style="color: #666666; font-style: italic;">// File already been downloaded?</span><br />
<span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&#39;file_downloaded&#39;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&#39;File has already been downloaded, please contact us if you have any problems&#39;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></p>
<p><span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span></p>
<p>&nbsp; <span style="color: #666666; font-style: italic;">// It&#39;s a valid transaction, update the database so that we know the file has been downloaded for next time</span><br />
&nbsp; <span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UPDATE transactions SET file_downloaded = 1 WHERE transaction_id=&#39;<span style="color: #009933; font-weight: bold;">%s</span>&#39;&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$transaction_id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&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: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></p>
<p>&nbsp; <span style="color: #666666; font-style: italic;">// Now force the file to be downloaded</span><br />
&nbsp; <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&#39;Content-Description: File Transfer&#39;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&#39;Content-Type: application/octet-stream&#39;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&#39;Content-Disposition: attachment; filename=&#39;</span><span style="color: #339933;">.</span><span style="color: #990000;">basename</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&#39;Content-Transfer-Encoding: binary&#39;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&#39;Expires: 0&#39;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&#39;Cache-Control: must-revalidate, post-check=0, pre-check=0&#39;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&#39;Pragma: public&#39;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&#39;Content-Length: &#39;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">filesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #990000;">ob_clean</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #990000;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #990000;">readfile</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></p>
<p><span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div>
]]></content:encoded>
			<wfw:commentRss>http://www.xeweb.net/2010/01/03/how-to-setup-one-time-download-of-your-files-securely/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Quick way to INSERT an array into a table</title>
		<link>http://www.xeweb.net/2009/12/30/quick-way-to-insert-an-array-into-a-table/</link>
		<comments>http://www.xeweb.net/2009/12/30/quick-way-to-insert-an-array-into-a-table/#comments</comments>
		<pubDate>Wed, 30 Dec 2009 10:10:18 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.xeweb.net/?p=76</guid>
		<description><![CDATA[This is a useful function that I wrote to insert an array into a table in your database. function insert_array&#40;$table, $data&#41; &#123; &#160; &#160;$cols = &#39;(&#39;; &#160;$values = &#39;(&#39;; &#160;foreach &#40;$data as $key=&#62;$value&#41; &#123; &#160; $value = addslashes&#40;$value&#41;; &#160; $cols .= &#34;$key,&#34;; &#160; &#160; $values .= &#34;&#39;$value&#39;,&#34;; &#160; &#160;&#125; &#160;$cols = rtrim&#40;$cols, &#39;,&#39;&#41;.&#39;)&#39;; &#160;$values = [...]]]></description>
			<content:encoded><![CDATA[<p>This is a useful function that I wrote to insert an array into a table in your database.</p>
<div class="php geshi no php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> insert_array<span style="color: #009900;">&#40;</span><span style="color: #000088;">$table</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp;<span style="color: #000088;">$cols</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&#39;(&#39;</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #000088;">$values</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&#39;(&#39;</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <br />
&nbsp; <span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #990000;">addslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000088;">$cols</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$key</span>,&quot;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; <span style="color: #000088;">$values</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&#39;<span style="color: #006699; font-weight: bold;">$value</span>&#39;,&quot;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp;<span style="color: #000088;">$cols</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rtrim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cols</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&#39;,&#39;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&#39;)&#39;</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #000088;">$values</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rtrim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$values</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&#39;,&#39;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&#39;)&#39;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp;<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO <span style="color: #006699; font-weight: bold;">$table</span> <span style="color: #006699; font-weight: bold;">$cols</span> VALUES <span style="color: #006699; font-weight: bold;">$values</span>&quot;</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #b1b100;">return</span> <span style="color: #990000;">mysql_insert_id</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div>
<p>Here is an example of using this code:</p>
<div class="php geshi no php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000088;">$arr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&#39;title&#39;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&#39;title&#39;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&#39;product_code&#39;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&#39;product_code&#39;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&#39;description&#39;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&#39;description&#39;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&#39;price&#39;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&#39;price&#39;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$product_id</span> <span style="color: #339933;">=</span> insert_array<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;products&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div>
]]></content:encoded>
			<wfw:commentRss>http://www.xeweb.net/2009/12/30/quick-way-to-insert-an-array-into-a-table/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
