<?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>The Error Message &#187; PHP</title>
	<atom:link href="http://www.theerrormessage.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.theerrormessage.com</link>
	<description>Fix your error</description>
	<lastBuildDate>Sun, 18 Jul 2010 03:21:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>How to convert server date and time to the local date and time taking into account daylight saving time</title>
		<link>http://www.theerrormessage.com/2009/10/convert-server-time-to-local-time-taking-into-account-daylight-saving-time/</link>
		<comments>http://www.theerrormessage.com/2009/10/convert-server-time-to-local-time-taking-into-account-daylight-saving-time/#comments</comments>
		<pubDate>Sat, 31 Oct 2009 20:04:12 +0000</pubDate>
		<dc:creator>thalissar</dc:creator>
				<category><![CDATA[How to]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.theerrormessage.com/?p=39</guid>
		<description><![CDATA[&#8216;Many countries, or even parts of countries, adopt daylight saving time (DST, also known as &#8220;Summer Time&#8221;) during part of the year. This typically involves advancing clocks by an hour near the start of spring and adjusting back in autumn (&#8220;spring&#8221; forward, &#8220;fall&#8221; back).&#8217; (see Wikipedia) Now I will demonstrate how we can convert a [...]]]></description>
			<content:encoded><![CDATA[<p>&#8216;Many countries, or even parts of countries, adopt daylight saving time (DST, also known as &#8220;Summer Time&#8221;) during part of the year. This typically involves advancing clocks by an hour near the start of spring and adjusting back in autumn (&#8220;spring&#8221; forward, &#8220;fall&#8221; back).&#8217; (see <a title="Time zone, Daylight saving time" href="http://en.wikipedia.org/wiki/Time_zone#Daylight_saving_time" target="_blank">Wikipedia</a>)</p>
<p>Now I will demonstrate how we can convert a server date and time to the corresponding date and time in another timezone in Europe, taking Romania as example.</p>
<p>&#8216;All countries in Europe except Iceland observe DST, and most change on the same date and time, starting on the last Sunday in March and ending on the last Sunday in October.&#8217;<br />
&#8216;In the West European (UTC), Central European (CET, UTC+1), and East European (UTC+2) time zones the change is simultaneous: on both dates the clocks are changed everywhere at 01:00 UTC, i.e. from local times of 01:00/02:00/03:00 to 02:00/03:00/04:00 in March, and vice versa in October.&#8217; (see <a title="Time zone, Daylight saving time in Europe" href="http://en.wikipedia.org/wiki/Daylight_saving_time_around_the_world#In_general" target="_blank">Wikipedia</a>)</p>
<p>Romania is a country in the Eastern Europe, which falls under the East European Time (EET) also known as UTC+2 time zone and uses Eastern European Summer Time (UTC+3) as a summer daylight saving time.</p>
<p>The next function will be used to find out the offset in hours from UTC (GMT) of a local date and time of Romania or another region that switches between DST and standard time on the same date and time as Romania (is the local date and time in DST or in standard time?)</p>
<p><code class="php">function local_offset_from_utc($timestamp, $standard_offset)<br />
//$timestamp - the timestamp of the server date and time we want to convert to local date and time<br />
//$standard_offset - the offset from UTC of the local date and time when observing the standard time; in daylight saving time the offset is $standard_offset + 1<br />
{<br />
	$year = gmdate("Y", $timestamp); //get the UTC year of the given date and time<br />
//get the date to 'spring forward'<br />
	$mar_31_utc_ts = gmmktime(0, 0, 0, 4, 0, $year); //the UTC timestamp of March 31, the last day of March, equivalent to gmmktime(0, 0, 0, 3, 31, $year)<br />
	$last_sun_mar = 31 - gmdate("w", $mar_31_utc_ts); //the day of the month under which falls the last Sunday in March<br />
	$spring_ts = gmmktime(1, 0, 0, 3, $last_sun_mar, $year); //the timestamp of the last Sunday in March 01:00 UTC<br />
//get the date to 'fall back'<br />
	$oct_31_utc_ts = gmmktime(0, 0, 0, 11, 0, $year); //the UTC timestamp of October 31, the last day of October, equivalent to gmmktime(0, 0, 0, 10, 31, $year)<br />
	$last_sun_oct = 31 - gmdate("w", $oct_31_utc_ts); //the day of the month under which falls the last Sunday in October<br />
	$fall_ts = gmmktime(1, 0, 0, 10, $last_sun_oct, $year); //the timestamp of the last Sunday in October, 01:00 UTC<br />
	if($timestamp >= $spring_ts &#038;&#038; $timestamp < $fall_ts) //daylight saving time<br />
	{<br />
		return $standard_offset + 1;<br />
	}<br />
//standard time<br />
	return $standard_offset;<br />
}</code></p>
<p>If the server switches between DST and standard time simultaneously with your timezone, you can simply use date("I") to find out if your local date and time is in daylight saving time or standard time:</p>
<p><code class="php">if(date("I", $timestamp) == 1)<br />
{<br />
	$offset = $standard_offset + 1;<br />
}<br />
else<br />
{<br />
	$offset = $standard_offset;<br />
}</code></p>
<p>Be careful with that, because a sever don't always switch between DST and standard time at the same hour as the timezone it has set, for example my server switches from DST to standard time at 03:00 UTC+3, which becomes 02:00 UTC+2, when it should go from 04:00 UTC+3 to 03:00 UTC+2). So I better use the function above that works not caring if or when the timezone of the server switches between DST and standard time.</p>
<p>If an offset of a timezone from UTC is negative, then the UTC date and time is greater than the corresponding date and time in the considered timezone; if the offset is positive then the UTC date and time is lower than the corresponding date and time in the considered timezone. If we consider <em>t</em> being the date and time in a given timezone, <em>utc</em> being the corresponding UTC date and time and <em>offset</em> being the offset of the given timezone from UTC, then we have:</p>
<p><em>t = utc + offset<br />
utc = t - offset</em></p>
<p>So if we consider <em>t1</em> being a server date and time and <em>o1</em> being the offset of the timezone of the server from UTC (in hours) and <em>t2</em> being the local date and time and <em>o2</em> being the offset of the timezone of the server from UTC (in hours) we have:</p>
<p><em>utc = t1 - o1<br />
t2 = utc + o2 = t1 - o1 + o2</em></p>
<p>In practice we only have a server date and time which we convert to timestamp <em>ts1</em> based on which we find out the timestamp <em>ts2</em> when the date and time in the timezone of the server equaled/will equal to the local date and time corresponding to <em>ts1</em>:</p>
<p><em>ts2 = ts1 - os1 + os2</em>, where <em>os1 = o1 * 60 *60</em> seconds and <em>os2 = o2 * 60 *60</em> seconds</p>
<p>E.g. Let's consider the timezone of the server date and time is UTC-5 and Romania is in standard time (has an offset of 2 hours from UTC).</p>
<p>The UTC date and time that corresponds to the given timestamp equals to the server date and time that was/will be 5 hours later than the given timestamp:</p>
<p><em>ts_aux = ts1 - (-5 * 3600) = ts1 + 5 * 3600</em></p>
<p>The local date and time of Romania corresponding to the given timestamp equals to the UTC date and time that was/will be 2 hours later than the UTC date and time computed above:</p>
<p><em>ts2 = ts_aux + 2 * 3600 = ts1 + (5 + 2) * 3600 = ts1 + 7 * 3600</em></p>
<p>But this is NOT true if the server time and date that equals the needed local date and time falls under DST and the original server date falls under standard time or vice versa, which means that the offsets from UTC of the two server dates are different by 1 hour.</p>
<p>For example, if the server we considered before is in UTC-5 DST and goes back one hour on the first of November at 03:00:00 local server time and we want to see what Romania local date and time corresponds to the server date and time November 1st 01:00:00, then, applying the formula above, we will obtain a timstamp that corresponds to the server date and time November 1st 07:00:00 which should correspond to the local Romania time for the original server time, but the correct Romania date and time being November 1st 08:00:00. That is because, between November 1st 01:00:00 and November 1st 07:00:00, the server goes back one hour so that 03:00:00 DST becomes 02:00:00 standard time and 08:00:00 becomes 07:00:00, while the Romania time does not, Romania switching to DST one week earlier, so the correct formula in this case is:</p>
<p><em>ts2 = ts1 - os1 + os2 + 3600<br />
ts2 = ts1 + (5 + 2 + 1) * 3600 = ts1 + 8 * 3600</em></p>
<p>Similarly, if the server switches from standard time to DST between the two dates, the formula becomes:</p>
<p><em>ts2 = ts1 - os1 + os2 - 3600<br />
ts2 = ts1 + (5 + 2 - 1) * 3600 = ts1 + 6 * 3600</em></p>
<p>The next function is used to determine what the timestamp was/will be in the timezone of the server when the server date and time equaled/will be equal to the needed local date and time.</p>
<p><code class="php">function get_timestamp_for_local($date = "", $standard_offset = 2)<br />
{<br />
	if(!empty($date))<br />
	{<br />
		$timestamp = strtotime($date);<br />
	}<br />
	else<br />
	{<br />
		$timestamp = time();<br />
	}<br />
	$os1 = date("Z", $timestamp);<br />
	$os2 = local_offset_from_utc($timestamp, $standard_offset) * 3600;<br />
	$ts = $timestamp - $os1 + $os2;<br />
	//check the offset from UTC of the server date and time that equal the needed local date and time<br />
	$os3 = date("Z", $ts);<br />
	if($os3 == $os1) //the two server dates are both in DST or both in standard time<br />
	{<br />
		return $ts;<br />
	}<br />
	elseif($os3 > $os1) //the server date and time that equal the needed local date and time are in DST and the original server date and time are in standard time<br />
	{<br />
		return $ts - 3600;<br />
	}<br />
 	//the server date and time that equal the needed local date and time are in standard time and the original server date and time are in DST<br />
	return $ts + 3600;<br />
}</code></p>
<p>And finally we get the needed date and time:</p>
<p><code class="php">function get_ro_date($date)<br />
{<br />
	return date("d.m.Y H:i:s", get_timestamp_for_local($date));<br />
}</code></p>
<p>Next l will show how we can convert a server date and time to the corresponding date and time in another timezone in North America, taking the State of Washington as example.</p>
<p>'North America generally follows the same procedure, with each time zone switching at 02:00 LST (local standard time) to 03:00 LDT (local daylight time) on the second Sunday in March, and back from 02:00 LDT to 01:00 LST on the first Sunday in November since 2007.'<br />
In the United States of America, 'the start of DST now occurs on the second Sunday in March and ends on the first Sunday in November.' (see <a title="Daylight saving time in North America" href="http://en.wikipedia.org/wiki/Daylight_saving_time_around_the_world#North_America" target="_blank">Wikipedia</a>)</p>
<p>The State of Washington falls under the Pacific Time Zone (PT), which is Pacific Standard Time (PST) or UTC-8 when observing standard time and Pacific Daylight Time (PDT) or UTC-7 during daylight saving time.</p>
<p>So we have to change the previous <em>local_offset_from_utc</em> function to find out the offset in hours from UTC (GMT) of a local date and time of Washington:</p>
<p><code class="php">function local_offset_from_utc2($timestamp, $standard_offset, $spring_forward_hour, $fall_back_hour)<br />
//$timestamp - the timestamp of the server date and time we want to convert to local date and time<br />
//$standard_offset - the offset from UTC of the local date and time when observing the standard time; in daylight saving time the offset is $standard_offset + 1<br />
//$spring_forward_hour - the hour of the local time when switching to DST<br />
//$fall_back_hour - the hour of the local time when switching to standard time<br />
{<br />
//find out the year of the local date and time corresponding to the given server date and time, based on the corresponding UTC date and time<br />
	$year = gmdate("Y", $timestamp); //UTC year of the given server date and time<br />
	if($standard_offset &lt; 0) //local timezone is behind UTC<br />
	{<br />
		//if the given sever date and time corresponds to January the 1st in UTC and the UTC hour is lower than the absolute value of the offset of the local time from UTC, then the corresponding local date and time falls in the year previous to the UTC year<br />
		$month = gmdate("n", $timestamp);<br />
		if($month == 1)<br />
		{<br />
			$day = gmdate("j", $timestamp);<br />
			if($day == 1)<br />
			{<br />
				$hour = gmdate("G", $timestamp);<br />
				if($hour + $standard_offset &lt; 0)<br />
				{<br />
					$year -= 1;<br />
				}<br />
			}<br />
		}<br />
	}<br />
	else //local timezone time is ahead of UTC<br />
	{<br />
		//if the given sever date and time corresponds to December 31 in UTC and the sum of UTC hour and the offset of the local time from UTC is greater than 24, then the corresponding local date and time falls in the year subsequent to the UTC year<br />
		$month = gmdate("n", $timestamp);<br />
		if($month == 12)<br />
		{<br />
			$day = gmdate("j", $timestamp);<br />
			if($day == 31)<br />
			{<br />
				$hour = gmdate("G", $timestamp);<br />
				if($hour + $standard_offset &gt; 24)<br />
				{<br />
					$year += 1;<br />
				}<br />
			}<br />
		}<br />
	}<br />
	//get the date to 'spring forward'<br />
	$mar_1_utc_ts = gmmktime(0, 0, 0, 3, 1, $year); //the timestamp for the UTC time of March the 1st, 00:00:00<br />
	$mar_1_day_of_week = gmdate("w", $mar_1_utc_ts);<br />
	//compute the day of the month under which falls the second Sunday in March<br />
	if($mar_1_day_of_week &gt; 0) //the day of the week of the 1st of March is not Sunday<br />
	{<br />
		$second_sun_mar = 15 - $mar_1_day_of_week;<br />
	}<br />
	else<br />
	{<br />
		$second_sun_mar = 8;<br />
	}<br />
	$spring_local_ts = gmmktime($spring_forward_hour - $standard_offset, 0, 0, 3, $second_sun_mar, $year);<br />
	//get the date to 'fall back'<br />
	$nov_1_utc_ts = gmmktime(0, 0, 0, 11, 1, $year); //the timestamp for the UTC time of November the 1st, 00:00:00<br />
	$nov_1_day_of_week = gmdate("w", $nov_1_utc_ts);<br />
//compute the day of the month under which falls the first Sunday in November<br />
	if($nov_1_day_of_week &gt; 0) //the day of the week of the 1st of November is not Sunday<br />
	{<br />
		$first_sun_nov = 8 - $nov_1_day_of_week;<br />
	}<br />
	else<br />
	{<br />
		$first_sun_nov = 1;<br />
	}<br />
	$fall_local_ts = gmmktime($fall_back_hour - ($standard_offset + 1), 0, 0, 11, $first_sun_nov, $year);<br />
	if($timestamp &gt;= $spring_local_ts &amp;&amp; $timestamp &lt; $fall_local_ts) //daylight saving time<br />
	{<br />
		return $standard_offset + 1;<br />
	}<br />
	//standard time<br />
	return $standard_offset;<br />
}<br />
</code></p>
<p>The function <em>get_timestamp_for_local</em> remains the same, only we add 2 more paramters to it which will be needed when calling  the <em></em> function:</p>
<p><code class="php">function get_timestamp_for_local2($date = "", $standard_offset = -8, $spring_forward_hour = 2, $fall_back_hour = 3)<br />
{<br />
	if(!empty($date))<br />
	{<br />
		$timestamp = strtotime($date);<br />
	}<br />
	else<br />
	{<br />
		$timestamp = time();<br />
	}<br />
	$os1 = date("Z", $timestamp);<br />
	$os2 = local_offset_from_utc2($timestamp, $standard_offset, $spring_forward_hour, $fall_back_hour) * 3600;<br />
	$ts = $timestamp - $os1 + $os2;<br />
	//check the offset from UTC of the server date and time that equal the needed local date and time<br />
	$os3 = date("Z", $ts);<br />
	if($os3 == $os1) //the two dates are both in DST or both in standard time<br />
	{<br />
		return $ts;<br />
	}<br />
	elseif($os3 > $os1) //the server date and time that equal the needed local date and time are in DST and the original server date and time are in standard time<br />
	{<br />
		return $ts - 3600;<br />
	}<br />
	//the server date and time that equal the needed local date and time are in standard time and the original server date and time are in DST<br />
	return $ts + 3600;<br />
}</code></p>
<p>And now we can find out the date and time in the State of Washington that corresponds to the given server date and time:</p>
<p><code class="php">function get_wa_date($date)<br />
{<br />
	return date("Y-m-d H:i:s", get_timestamp_for_local2($date));<br />
}</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.theerrormessage.com/2009/10/convert-server-time-to-local-time-taking-into-account-daylight-saving-time/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SoftException in Application.cpp:544: Directory &#8220;/path/dir&#8221; is writeable by group</title>
		<link>http://www.theerrormessage.com/2009/10/softexception-in-application-cpp544-directory-pathdir-is-writeable-by-group/</link>
		<comments>http://www.theerrormessage.com/2009/10/softexception-in-application-cpp544-directory-pathdir-is-writeable-by-group/#comments</comments>
		<pubDate>Fri, 02 Oct 2009 16:14:53 +0000</pubDate>
		<dc:creator>gbl</dc:creator>
				<category><![CDATA[Apache Web Server]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.theerrormessage.com/?p=25</guid>
		<description><![CDATA[Full error message: SoftException in Application.cpp:544: Directory &#8220;/path/to/directory&#8221; is writeable by group The error apeared in cpanel logs at a hosting company and was related to a directory I created via ftp and where I tried to run some php script. When accessing the script from the web browser i got the error &#8220;500 Internal [...]]]></description>
			<content:encoded><![CDATA[<p>Full error message: <em>SoftException in Application.cpp:544: Directory &#8220;/path/to/directory&#8221; is writeable by group</em></p>
<p>The error apeared in cpanel logs at a hosting company and was related to a directory I created via ftp and where I tried to run some php script. When accessing the script from the web browser i got the error &#8220;500 Internal Server Error&#8221;</p>
<p>The problem is that Apache 2.0 server doesn&#8217;t allow in some cases (i don&#8217;t know these cases yet) that the directories created with write access rights set to all unless it&#8217;s set by the same unix user as the user running apache process. In order to fix this we need to create the folder from the same user as apache user. To do this we need to create the folder or to change the access rights from php.</p>
<p>The fix is to upload a small php file with the following code:</p>
<p><code class="php">&lt;?php<br />
chmod("./dir", 0755);<br />
?&gt;</code></p>
<p>After we set the access right on &#8220;dir&#8221; to 777 we run the  php script then the error should not appear anymore.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.theerrormessage.com/2009/10/softexception-in-application-cpp544-directory-pathdir-is-writeable-by-group/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>When submitting a PHP form, the script does not stop</title>
		<link>http://www.theerrormessage.com/2009/08/when-submitting-a-php-form-the-script-does-not-stop/</link>
		<comments>http://www.theerrormessage.com/2009/08/when-submitting-a-php-form-the-script-does-not-stop/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 17:43:29 +0000</pubDate>
		<dc:creator>thalissar</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.theerrormessage.com/2009/08/17/when-submitting-a-php-form-the-script-does-not-stop/</guid>
		<description><![CDATA[If you submit a PHP form which passes a large amount of data using the POST method, like a file upload form, it would be a good idea to increase the value of the PHP configuration variable post_max_size (you will find it in the php.ini file) if it seems like the script is running forever. [...]]]></description>
			<content:encoded><![CDATA[<p>If you submit a PHP form which passes a large amount of data using the POST method, like a file upload form, it would be a good idea to increase the value of the PHP configuration variable <em>post_max_size</em> (you will find it in the php.ini file) if it seems like the script is running forever.  <em>post_max_size</em> should be set to a value larger than the maximum sum of the sizes of the files the script is used to upload.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.theerrormessage.com/2009/08/when-submitting-a-php-form-the-script-does-not-stop/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Importing a MySQL data file using phpMyAdmin results in incomplete data in MySQL</title>
		<link>http://www.theerrormessage.com/2009/08/importing-a-mysql-data-file-using-phpmyadmin-results-in-incomplete-data-in-mysql/</link>
		<comments>http://www.theerrormessage.com/2009/08/importing-a-mysql-data-file-using-phpmyadmin-results-in-incomplete-data-in-mysql/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 16:56:42 +0000</pubDate>
		<dc:creator>thalissar</dc:creator>
				<category><![CDATA[Apache Web Server]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.theerrormessage.com/2009/08/17/importing-a-mysql-data-file-using-phpmyadmin-results-in-incomplete-data-in-mysql/</guid>
		<description><![CDATA[I tried moving all my databases from MySQL 4.1 to MySQL 5.1 on Windows and since I had some InnoDB tables I could not just copy the data files between MySQL Server versions (from C:\Documents and Settings\All Users\Application Data\MySQL\MySQL 4.1\data\ to  C:\Documents and Settings\All Users\Application Data\MySQL\MySQL 5.1\data\). So, after I exported the data from the [...]]]></description>
			<content:encoded><![CDATA[<p>I tried moving all my databases from MySQL 4.1 to MySQL 5.1 on Windows and since I had some InnoDB tables I could not just copy the data files between MySQL Server versions (from C:\Documents and Settings\All Users\Application Data\MySQL\MySQL 4.1\data\ to  C:\Documents and Settings\All Users\Application Data\MySQL\MySQL 5.1\data\).<br />
So, after I exported the data from the old MySQL Server version I tried importing it to the MySQL 5.1 using phpMyAdmin.<br />
The file to import was 42MB in size, so I had to set some PHP configuration variables (in php.ini file) to greater values:<br />
<code>post_max_size = 64M<br />
upload_max_filesize = 64M</code></p>
<p>At first it looked like the phpMyAdmin import script entered a loop or something, as it wouldn&#8217;t stop even after running for several minutes and, as I was looking into the data files it was producing, no new files appeared. It was like it blocked on a table with a large number of records, which didn&#8217;t make any sense. Then I restarted the Apache server and I tried the import all over again. The same thing was happening. I finally decided to let it run for more time and the script finally stopped. But the data imported was incomplete; a part of the records in the large table I thought script blocked on, and every table and database from there on were missing, with no PHP or MySQL error returned.<br />
Then I thought the script needed even more time to run, so I increased the value of the PHP configuration variable <em>max_execution_time</em>, but with the same result.</p>
<p><strong>The solution</strong>. It seems that the execution time limit for the import script in phpMyAdmin is defined by the variable <em>$cfg['ExecTimeLimit']</em> in &lt;path_to_phpMyAdmin&gt;/libraries/config.default.php and the value of this variable should be a lot greater than its default value of 300 seconds when importing a great amount of data. In my case it needed about an hour (3600 seconds) to complete. So you have to set this variable depending on the quantity of data you are importing.<br />
You might also need to set the MySQL configuration variable <em>max_allowed_packet</em> (in my case, in C:\Program Files\MySQL\MySQL Server 5.1\my.ini) to a greater value, if you have large queries in the data file you want to import, for avoiding MySQL error 2006, &#8216;mysql server has gone away&#8217;.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.theerrormessage.com/2009/08/importing-a-mysql-data-file-using-phpmyadmin-results-in-incomplete-data-in-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Php script redirects even when output started before calling header() function</title>
		<link>http://www.theerrormessage.com/2008/11/php-script-redirects-even-when-output-started-before-calling-header-function/</link>
		<comments>http://www.theerrormessage.com/2008/11/php-script-redirects-even-when-output-started-before-calling-header-function/#comments</comments>
		<pubDate>Wed, 05 Nov 2008 18:31:27 +0000</pubDate>
		<dc:creator>gbl</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.theerrormessage.com/2008/11/05/php-script-redirects-even-when-output-started-before-calling-header-function/</guid>
		<description><![CDATA[This happens because of output_buffering is on. In order to turn off output buffering modify php.ini if you have access and set: output_buffering = Off or add in the local directory a .htaccess file containing this line: php_value output_buffering Off   # or 0]]></description>
			<content:encoded><![CDATA[<p>This happens because of output_buffering is on.</p>
<p>In order to turn off output buffering modify php.ini if you have access and set:</p>
<p><code>output_buffering = Off</code></p>
<p>or add in the local directory a .htaccess file containing this line:</p>
<p><code>php_value output_buffering Off   # or 0 </code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.theerrormessage.com/2008/11/php-script-redirects-even-when-output-started-before-calling-header-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>mysqldump &#8211; with exec() function from php outputs empty file</title>
		<link>http://www.theerrormessage.com/2008/10/mysqldump-with-exec-function-from-php-outputs-empty-file/</link>
		<comments>http://www.theerrormessage.com/2008/10/mysqldump-with-exec-function-from-php-outputs-empty-file/#comments</comments>
		<pubDate>Tue, 21 Oct 2008 13:25:33 +0000</pubDate>
		<dc:creator>gbl</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[mysqldump]]></category>

		<guid isPermaLink="false">http://www.theerrormessage.com/2008/10/21/mysqldump-with-exec-function-from-php-outputs-empty-file/</guid>
		<description><![CDATA[This error occurs on any operating system (windows, linux). The problem is that instead of getting a sql file with the database data you get a empty (0 kb.) file. So we have the following code: $command = "mysqldump --opt --skip-extended-insert --complete-insert -h ".$DB_HOST." -u ".$DB_USER." -p ".$DB_PASS." ".$DB_NAME." &#62; backup.sql"; exec($command, $ret_arr, $ret_code); echo [...]]]></description>
			<content:encoded><![CDATA[<p>This error occurs on any operating system (windows, linux). The problem is that instead of getting a sql file with the database data you get a empty (0 kb.) file.</p>
<p>So we have the following code:</p>
<p><code class="php">$command = "mysqldump --opt --skip-extended-insert --complete-insert -h ".$DB_HOST." -u ".$DB_USER." -p ".$DB_PASS." ".$DB_NAME." &gt; backup.sql";</p>
<p>exec($command, $ret_arr, $ret_code);<br />
echo "ret_arr: &lt;br /&gt;";<br />
print_r($ret_arr);<br />
</code><br />
and we get an empty file and no output.</p>
<p>So we will fix this error in a few steps:</p>
<p>1. First we need to make sure that we have access to mysqldump command. For Linux machines this command is accessible from anywhere if not you will have to find the place where mysqldump file is (usually the bin folder of mysql).</p>
<p>In order to do this we have to get some output from our command so we will strip all the options from the command and we will remain with this:</p>
<p><code class="php">$command = "mysqldump"; // mysqldump.exe on Windows</code></p>
<p>So execute the php script. It&#8217;s ok if you get output like this:</p>
<pre>Array
(
    [0] =&gt; Usage: mysqldump [OPTIONS] database [tables]
    [1] =&gt; OR     mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
    [2] =&gt; OR     mysqldump [OPTIONS] --all-databases [OPTIONS]
    [3] =&gt; For more options, use mysqldump --help
)</pre>
<p>If you don&#8217;t see something like that then you must check to see the path to the mysqldump command.</p>
<p>If you are in windows make sure you append the full path to the command. If you have a folder like I have C:\\Program Files\\MySQL\\MySQL Server 4.1\\bin\\mysqldump.exe with spaces in it you must make sure that you enclose the command between quotes like this:</p>
<p><code class="php">$command = "\"C:\\Program Files\\MySQL\\MySQL Server 4.1\\bin\\mysqldump.exe\" --opt --skip-extended-insert --complete-insert -h ".$DB_HOST." -u ".$DB_USER." -p ".$DB_PASS." ".$DB_NAME." &gt; backup.sql";</code></p>
<p>If append the right path to the command and you still cannot get the output then this article can&#8217;t help.</p>
<p>2.  Make sure you have the rights to create the sql file. This step is mostly for Linux machines where it is very possible that you may try to create a file from php in a folder where you don&#8217;t have writing rights.</p>
<p>So to test this after the previous step is done you can do the following: append to the previous command extra options so that the output is not returned but instead written in a file. So we have the previous command:</p>
<p>In Windows:</p>
<p><code class="php">$command = "\"C:\\Program Files\\MySQL\\MySQL Server 4.1\\bin\\mysqldump.exe\" &gt; backup.sql";</code></p>
<p>In Linux:</p>
<p><code class="php">$command = "mysqldump &gt; backup.sql";</code></p>
<p>After running the file &#8220;backup.sql&#8221; should  be created.</p>
<p>3.  You must now correct your statement. This means that we must <strong>use the long versions of the options</strong> like this:</p>
<p>Windows:</p>
<p><code class="php">$command = "\"C:\\Program Files\\MySQL\\MySQL Server 4.1\\bin\\mysqldump.exe\" --opt --skip-extended-insert --complete-insert --host=".$DB_HOST." --user=".$DB_USER." --password=".$DB_PASS." ".$DB_NAME." &gt; backup.sql";</code></p>
<p>Linux:</p>
<p><code class="php">$command = "mysqldump --opt --skip-extended-insert --complete-insert --host=".$DB_HOST." --user=".$DB_USER." --password=".$DB_PASS." ".$DB_NAME." &gt; backup.sql";</code></p>
<p>The fix: <strong>The options for mysqldump when called from php must be in the longer version.</strong> Instead of &#8211;u use &#8211;user, instead of &#8211;p use &#8211;password and so on.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.theerrormessage.com/2008/10/mysqldump-with-exec-function-from-php-outputs-empty-file/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>The attachment of an email sent via PHP has 0 bytes</title>
		<link>http://www.theerrormessage.com/2008/09/the-attachment-of-an-email-sent-via-php-has-0-bytes/</link>
		<comments>http://www.theerrormessage.com/2008/09/the-attachment-of-an-email-sent-via-php-has-0-bytes/#comments</comments>
		<pubDate>Tue, 09 Sep 2008 13:41:47 +0000</pubDate>
		<dc:creator>thalissar</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.theerrormessage.com/2008/09/09/the-attachment-of-an-email-sent-via-php-has-0-bytes/</guid>
		<description><![CDATA[I wanted to send an email that only contained an attachment as follows: $headers = "MIME-Version: 1.0\r\n"; $headers .= "To: &#60;".$to_email."&#62;\r\n"; $headers .= "From: ".$from_name." &#60;".$from_email."&#62;"; $random_hash = md5(date('r', time())); // add boundary string and mime type specification $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; // read the atachment file contents from a string previously formed, // [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted to send an email that only contained an attachment as follows:</p>
<p><code class="php">$headers  = "MIME-Version: 1.0\r\n";<br />
$headers .= "To: &lt;".$to_email."&gt;\r\n";<br />
$headers .= "From: ".$from_name." &lt;".$from_email."&gt;";<br />
$random_hash = md5(date('r', time()));<br />
// add boundary string and mime type specification<br />
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";<br />
// read the atachment file contents from a string previously formed,<br />
// encode it with MIME base64,<br />
// and split it into smaller chunks<br />
$attachment = chunk_split(base64_encode($content));<br />
// construct the body of the message<br />
$message = "--PHP-mixed-".$random_hash;<br />
// my attachment was an html file<br />
$message .= "\r\nContent-Type: text/html; name=\"".$filename."\"";<br />
$message .= "\r\nContent-Transfer-Encoding: base64";<br />
$message .= "\r\nContent-Disposition: attachment\r\n".$attachment."\r\n";<br />
$message .= "\r\n--PHP-mixed-".$random_hash."--";<br />
// Windows<br />
ini_set('sendmail_from', $return_email);<br />
mail($to_email, addslashes($subject), $message, $headers);<br />
ini_restore('sendmail_from');<br />
// Linux<br />
// mail($to_email, addslashes($subject), $message, $headers, "-r ".$return_email);</code></p>
<p>I received an email with an attachment, but the problem was that the attached file had 0 bytes.<br />
To my surprise, after comparing the source of the email with the source of an email with a complete attachment, I found out the problem: before the actual content of the attachment ($attachment) there should be an empty line.<br />
I replaced the line of code:<br />
<code class="php">$message .= "\r\nContent-Disposition: attachment\r\n".$attachment."\r\n";</code><br />
with:<br />
<code class="php">$message .= "\r\nContent-Disposition: attachment\r\n\r\n".$attachment."\r\n"; // notice the double \r\n</code><br />
and it worked! This way I received the email with the correct attachment.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.theerrormessage.com/2008/09/the-attachment-of-an-email-sent-via-php-has-0-bytes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to use specific language characters with PHP and MySQL (example: Romanian)</title>
		<link>http://www.theerrormessage.com/2007/12/how-to-use-specific-language-characters-with-php-and-mysql-example-romanian/</link>
		<comments>http://www.theerrormessage.com/2007/12/how-to-use-specific-language-characters-with-php-and-mysql-example-romanian/#comments</comments>
		<pubDate>Fri, 14 Dec 2007 00:54:15 +0000</pubDate>
		<dc:creator>gbl</dc:creator>
				<category><![CDATA[How to]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.theerrormessage.com/2007/12/13/how-to-use-specific-language-characters-with-php-and-mysql-example-romanian/</guid>
		<description><![CDATA[Problem: Using specific characters from European languages like Romanian, Bulgarian, Czech and so on (usually the ones without support in ISO 8859-1) rises errors when displaying the content in browsers turning special characters in unrecognizable ones. My fix for this problem is using UTF-8 character set encoding for every page of the website and the [...]]]></description>
			<content:encoded><![CDATA[<p>Problem: <em>Using specific characters from European languages like Romanian, Bulgarian, Czech and so on (usually the ones without support in ISO 8859-1) rises errors when displaying the content in browsers turning special characters in unrecognizable ones</em>.</p>
<p>My fix for this problem is using UTF-8 character set encoding for every page of the website and the MySQL tables that contain the fields you are using. Also all the html encodings from PHP use the UTF-8 character set encoding (this is not mandatory).</p>
<p>If you already have the database, but with the default character set (<em>latin1</em>) and collation <em>(latin1_swedish_ci</em>) for the tables with text fields (of type CHAR, VARCHAR, TEXT etc) in which you need to have special characters, you should change the character set of each of those tables like this:</p>
<p><code class="mysql">ALTER TABLE my_table CONVERT TO CHARACTER SET utf8;</code></p>
<p>If you don&#8217;t have the database then you should create it and when you create a table that you need to use with specific language characters, you should specify the  character set for that table:</p>
<p><code class="mysql">CREATE TABLE `my_table` (<br />
`idmy_table` tinyint(3) unsigned NOT NULL auto_increment,<br />
`my_field` varchar(255) NOT NULL default '',<br />
PRIMARY KEY  (`idmy_table`)<br />
) CHARSET=utf8;</code></p>
<p>The most important thing is that in PHP, after opening a database connection, before executing any query to the database, you should ensure that this code is executed <code class="php">mysql_query("SET NAMES utf8", $my_conn);</code>This tells the server what character set the client is using for sending SQL statements and the character set the server should use to return the results to the client.</p>
<p>A simple example:<br />
<code class="html">&lt;?php<br />
$my_conn = @mysql_connect("localhost", "user", "pass")<br />
or die("There was a problem connecting to MySQL. Please try again later.");<br />
if(!@mysql_select_db("test", $my_conn))<br />
{<br />
die ("There was a problem connecting to the database. Please try again later.");<br />
}<br />
mysql_query("SET NAMES utf8", $my_conn);<br />
if(!empty($_GET['mystr']))<br />
{<br />
// insert the string into the database<br />
$str = htmlspecialchars($_GET['mystr'], ENT_QUOTES, "UTF-8");<br />
$query = "INSERT INTO my_table_t (my_field) VALUES('".$str."')";<br />
$result = mysql_query($query, $my_conn);<br />
if($result)<br />
{<br />
// save the id of the table row inserted<br />
$last_insert_id = mysql_insert_id($my_conn);<br />
// get the last inserted value<br />
$query = "SELECT my_field FROM my_table_t WHERE idmy_table = '".$last_insert_id."'";<br />
$result = mysql_query($query, $my_conn);<br />
if($result &amp;&amp; $row = mysql_fetch_array($result, MYSQL_ASSOC))<br />
{<br />
$db_string = $row['my_field'] ;<br />
}<br />
}<br />
}<br />
elseif(!empty($_GET['searchstr']))<br />
{<br />
$str = htmlspecialchars($_GET['searchstr'], ENT_QUOTES, "UTF-8");<br />
$query = "SELECT * FROM my_table_t WHERE my_field LIKE '%".$str."%'";<br />
$result = mysql_query($query, $my_conn);<br />
if($result)<br />
{<br />
while($row = mysql_fetch_array($result, MYSQL_ASSOC))<br />
{<br />
$search_results[$row['idmy_table']] = $row['my_field'];<br />
}<br />
}<br />
}<br />
mysql_close($my_conn);<br />
?&gt;<br />
&lt;html&gt;<br />
&lt;head&gt;<br />
&lt;title&gt;Page title&lt;/title&gt;<br />
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;?<br />
if(!empty($db_string))<br />
{<br />
echo "&lt;strong&gt;Inserted string&lt;/strong&gt;: $db_string&lt;br /&gt;";<br />
}<br />
?&gt;<br />
&lt;form method="get" action=""&gt;<br />
String to insert into the database &lt;input type="text" name="mystr"/&gt;<br />
&lt;input type="submit" value="GO"/&gt;<br />
&lt;/form&gt;<br />
&lt;?<br />
if(!empty($search_results))<br />
{<br />
echo "&lt;strong&gt;Search results&lt;/strong&gt;:&lt;br /&gt;";<br />
foreach($search_results as $id =&gt; $value)<br />
{<br />
echo $value."&lt;br /&gt;";<br />
}<br />
}<br />
?&gt;<br />
&lt;form method="get" action=""&gt;<br />
Search query &lt;input type="text" name="searchstr"/&gt;<br />
&lt;input type="submit" value="GO"/&gt;<br />
&lt;/form&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;<br />
</code></p>
<p>Tip: The search in Romanian language over the database (tested with MySQL <strong>LIKE</strong> operator) works like a charm when searching words that have special characters or not.</p>
<p>For example: In Romanian language the word &#8220;peasant&#8221; is written as &#8220;ţăran&#8221; and someone who searches it gets the same result for the search terms &#8220;taran&#8221; or &#8220;ţăran&#8221; or &#8220;ţaran&#8221; or &#8220;tărân&#8221; and so on &#8211; so this is the real magic.</p>
<p><strong>UPDATE</strong>: You may also need to add a header to the php script if you use ob_start or similar php functions like this: <code class="php">header("Content-type: text/html; charset=UTF-8");</code> this usually fixes the encoding selection in Internet Explorer for this case.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.theerrormessage.com/2007/12/how-to-use-specific-language-characters-with-php-and-mysql-example-romanian/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>How to receive a failure notice when the recipient cannot be reached after sending an email using the PHP mail() function</title>
		<link>http://www.theerrormessage.com/2007/12/how-to-receive-a-failure-notice-when-the-recipient-cannot-be-reached-while-sending-an-email-using-the-php-mail-function-on-a-windows-server/</link>
		<comments>http://www.theerrormessage.com/2007/12/how-to-receive-a-failure-notice-when-the-recipient-cannot-be-reached-while-sending-an-email-using-the-php-mail-function-on-a-windows-server/#comments</comments>
		<pubDate>Sun, 09 Dec 2007 19:24:49 +0000</pubDate>
		<dc:creator>thalissar</dc:creator>
				<category><![CDATA[Apache Web Server]]></category>
		<category><![CDATA[How to]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.theerrormessage.com/2007/12/09/how-to-receive-a-failure-notice-when-the-recipient-cannot-be-reached-while-sending-an-email-using-the-php-mail-function-on-a-windows-server/</guid>
		<description><![CDATA[I could not receive a failure notice when sending email to an email address that does not exist ($to_address), using this code: $subject = "Email subject"; $message = "line1\r\nline2\r\nline3"; $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/plain; charset=iso-8859-1\r\n"; $headers .= "To: &#60;$to_email&#62;\r\n"; $headers .= "From: Me &#60;$my_email_address&#62;\r\n"; $headers .= "Return-Path: &#60;$my_return_email_address&#62;r\n"; mail($to_email, $subject, $message, $headers); [...]]]></description>
			<content:encoded><![CDATA[<p>I could not receive a failure notice when sending email to an email address that does not exist ($to_address), using this code:</p>
<p><code class="php">$subject = "Email subject";<br />
$message = "line1\r\nline2\r\nline3";<br />
$headers  = "MIME-Version: 1.0\r\n";<br />
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";<br />
$headers .= "To: &lt;$to_email&gt;\r\n";<br />
$headers .= "From: Me &lt;$my_email_address&gt;\r\n";<br />
$headers .= "Return-Path: &lt;$my_return_email_address&gt;r\n";<br />
mail($to_email, $subject, $message, $headers);</code></p>
<p>The problem was that the email address I wanted to receive the failure notices to ($my_return_email_address) was not the same as the value of the configuration option sendmail_from in the php.ini file (Apache web server installed on a machine with the Windows Professional operating system). So the failure notices were sent to the sendmail_from email address if this was an existing address, instead of the email address specified in the Return-path header of the email.</p>
<p>The solution is replacing the lines:</p>
<p><code class="php">$headers .= "Return-Path: &lt;$my_return_email_address&gt;r\n";<br />
mail($to_email, $subject, $message, $headers);</code></p>
<p>with:</p>
<p><code class="php">// when the PHP server runs on Windows<br />
ini_set(sendmail_from, $my_return_email_address);<br />
mail($to_email, $subject, $message, $headers);<br />
ini_restore(sendmail_from);</code><br />
<code class="php">// when the PHP server runs on UNIX<br />
mail($to_email, addslashes($subject), $message, $headers, "-r $my_return_email_address");</code></p>
<p>This means that, before sending the email, we set the value of the sendmail_from configuration option to $my_return_email_address and we restore the default value of the configuration option after the email is sent.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.theerrormessage.com/2007/12/how-to-receive-a-failure-notice-when-the-recipient-cannot-be-reached-while-sending-an-email-using-the-php-mail-function-on-a-windows-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Warning: mail(): SMTP server response: 451 See http://pobox.com/~djb/docs/smtplf.html. in path_to_php_file on line #</title>
		<link>http://www.theerrormessage.com/2007/12/warning-mail-smtp-server-response-451-see-httppoboxcomdjbdocssmtplfhtml-in-path_to_php_file-on-line/</link>
		<comments>http://www.theerrormessage.com/2007/12/warning-mail-smtp-server-response-451-see-httppoboxcomdjbdocssmtplfhtml-in-path_to_php_file-on-line/#comments</comments>
		<pubDate>Sat, 08 Dec 2007 16:44:54 +0000</pubDate>
		<dc:creator>thalissar</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.theerrormessage.com/2007/12/08/warning-mail-smtp-server-response-451-see-httppoboxcomdjbdocssmtplfhtml-in-path_to_php_file-on-line/</guid>
		<description><![CDATA[Full error message: Warning: mail(): SMTP server response: 451 See http://pobox.com/~djb/docs/smtplf.html. in path_to_php_file on line # This is an error message that I got lots of times and it was always the same problem. So I decided to write here the solution for the next time I encounter it and I thought it might also [...]]]></description>
			<content:encoded><![CDATA[<p>Full error message: <em>Warning: mail(): SMTP server response: 451 See http://pobox.com/~djb/docs/smtplf.html. in path_to_php_file on line #</em></p>
<p>This is an error message that I got lots of times and it was always the same problem. So I decided to write here the solution for the next time I encounter it and I thought it might also help other people.</p>
<p>So I tried to send an email in plain text using the PHP function mail() :</p>
<p><code class="php">// consider this being an existing email address<br />
$to_address = "abc@def.com";<br />
$subject = "Email subject";<br />
$message = "line1<br />
line2<br />
line3";<br />
$headers  = "MIME-Version: 1.0\r\n";<br />
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";<br />
$headers .= "To: &lt;".$to_email."&gt;\r\n";<br />
$headers .= "From: Me &lt;".$my_email_address."&gt;\r\n";<br />
mail($to_email, $subject, $message, $headers);</code></p>
<p>It worked fine with Apache server running on Linux operating system, but I got the error message mentioned before with the server running on Windows.<br />
It looks like while Unix-based systems recognize \n character (the equivalent in PHP of LF &#8211; line feed) as a newline even when sending emails, Windows systems are stricter in comunicating using some textual Internet Protocols (such as SMTP) that mandate the line terminator to be the ASCII CR+LF (carriage return + line feed) sequence, which is abstracted in PHP to \r\n character sequence.</p>
<p>So the problem was the $message variable. The correct way to assign the multiple lines value to it is:</p>
<p><code class="php">$message = "line1\r\nline2\r\nline3";  // separate the lines with \r\n<br />
// or,  elegantly:<br />
$message = "line1\r\n";<br />
$message .= "line2\r\n";<br />
$message .= "line3";</code></p>
<p>And now it works on Windows, too.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.theerrormessage.com/2007/12/warning-mail-smtp-server-response-451-see-httppoboxcomdjbdocssmtplfhtml-in-path_to_php_file-on-line/feed/</wfw:commentRss>
		<slash:comments>58</slash:comments>
		</item>
	</channel>
</rss>
