<?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>Lukas Wojcik - Web Developer &#187; PHP</title>
	<atom:link href="http://blog.lukaswojcik.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.lukaswojcik.com</link>
	<description>News, Tricks and How-To&#039;s</description>
	<lastBuildDate>Fri, 23 Oct 2015 10:02:30 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.39</generator>
	<item>
		<title>Stay logged in with PHP Sessions</title>
		<link>http://blog.lukaswojcik.com/stay-logged-in-with-php-sessions/</link>
		<comments>http://blog.lukaswojcik.com/stay-logged-in-with-php-sessions/#comments</comments>
		<pubDate>Fri, 23 Oct 2015 08:46:28 +0000</pubDate>
		<dc:creator><![CDATA[Luky]]></dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.lukaswojcik.com/?p=28</guid>
		<description><![CDATA[Building own applications When you love building everything yourself and dont want to depend on frameworks you most likely came across the need of building a login system that has the capability to let the user stay logged in into your site. The straight forward way would be to use PHP build in _SESSION System &#8230; <a href="http://blog.lukaswojcik.com/stay-logged-in-with-php-sessions/" class="more-link">Continue reading <span class="screen-reader-text">Stay logged in with PHP Sessions</span></a>]]></description>
				<content:encoded><![CDATA[<h2><strong>Building own applications</strong></h2>
<p>When you love building everything yourself and dont want to depend on frameworks you most likely came across the need of building a login system that has the capability to let the user stay logged in into your site.</p>
<p>The straight forward way would be to use PHP build in _SESSION System to store login information. But this session system has a garbage collection which cleans the session data from time to time.</p>
<h3><strong>STAY LOGGEDIN!</strong></h3>
<p>First of all what you can do to lower that effect is to set the garbage interval very high:</p>
<pre class="prettyprint"><a href="http://php.net/manual/en/session.configuration.php#ini.session.gc-maxlifetime">session.gc_maxlifetime</a> = 65535
</pre>
<p>Think this will solve your problem? Nope.</p>
<p><span id="more-28"></span><br />
Your users will only log out later but won&#8217;t stay logged into your site.</p>
<p><strong>The solution </strong>is to use own session handlers for this. I prefare to use MySQL to store session data.</p>
<h4>Create a session.php file with this code:</h4>
<pre class="prettyprint">&lt;?php

define("SESSION_LIFETIME", 60*60*24*30);

final class Session
{
 private $db;
 private $lifeTime;
 public function __construct(PDO $db)
 {
  $this-&gt;db = $db;
  $this-&gt;lifeTime = SESSION_LIFETIME;
 }
 public function db_q($q, array $r)
 {
  try {
   $stmt = $this-&gt;db-&gt;prepare($q);
   $stmt-&gt;execute($r);
   $errInfo = $stmt-&gt;errorInfo();
   if($errInfo[2] != "")
   {
    var_dump("DB - qrf - error - errorInfo(): [0]: ".$errInfo[0]."\n[1]: ".$errInfo[1]."\n\n[2]: ".$errInfo[2]." (Query: ".$q."\n");
    return false;
   }
   return true;
  }
  catch(PDOException $e) {
   return false;
  }
 }
 public function db_qrf($q, $r, $f)
 {
  try {
   $stmt = $this-&gt;db-&gt;prepare($q);
   $stmt-&gt;execute($r);
   $errInfo = $stmt-&gt;errorInfo();
   if($errInfo[2] != "")
   {
    var_dump("DB - db_qrf - error - errorInfo(): [0]: ".$errInfo[0]."\n[1]: ".$errInfo[1]."\n\n[2]: ".$errInfo[2]." (Query: ".$q."\n");
   }
   return $stmt-&gt;fetchColumn($f);
  }
  catch(PDOException $e) {
   return false;
  }
 }
 function open($savePath, $sessName)
 {
  return true;
 }
 function close()
 {
  $this-&gt;gc();
  return true;
 }
 function read($sessID)
 {
  return $this-&gt;db_qrf("SELECT `session_data` AS d from sessions WHERE session_id=? AND session_expires&gt;".time(), [$sessID], 0);
 }
 function write($sessID,$sessData)
 {
  $newExp = time()+$this-&gt;lifeTime;
  if($this-&gt;db_qrf("SELECT COUNT(*) from sessions WHERE session_id=?", [$sessID], 0) &gt; 0)
  {
   if($this-&gt;db_q("UPDATE sessions SET session_expires=?,session_data=?,IPv4=INET_ATON(?),Useragent=? WHERE session_id=?", [$newExp, $sessData, $_SERVER["REMOTE_ADDR"], $_SERVER["HTTP_USER_AGENT"], $sessID]))
   {
    return true;
   }
  }
  else
  {
   if($this-&gt;db_q("INSERT INTO sessions (session_id,session_expires,session_data,IPv4,Useragent) VALUES (?,?,?,?,?)", [$sessID, $newExp, $sessData, $_SERVER["REMOTE_ADDR"], $_SERVER["HTTP_USER_AGENT"]]))
   {
    return true;
   }
  }
  return false;
 }
 function destroy($sessID)
 {
  if($this-&gt;db_q("DELETE FROM sessions WHERE session_id=?", [$sessID]))
  {
   return true;
  }
  return false;
 }
 function gc()
 {
  $c = $this-&gt;db_qrf("SELECT COUNT(*) from sessions WHERE session_expires&lt;".time(), [], 0);
  if($c &gt; 0)
  {
   $this-&gt;db_q("DELETE from sessions WHERE session_expires&lt;".time(), []);
  }
  return $c;
 }
}

$db = new PDO("mysql:host=localhost;dbname=test", "root", "");
$session = new Session($db);
session_name("MYSESSION");
session_set_save_handler(array(&amp;$session,"open"), array(&amp;$session,"close"), array(&amp;$session,"read"), array(&amp;$session,"write"), array(&amp;$session,"destroy"), array(&amp;$session,"gc"));
session_start();

if(isset($_GET["l"]) &amp;&amp; $_GET["l"] == "1")
{
 session_destroy();
 echo "User logged out! &lt;a href=\"".str_replace("?l=1", "",  $_SERVER["REQUEST_URI"])."\"&gt;Login again&lt;/a&gt;";
}
elseif(!isset($_SESSION["loggedin"]))
{
 $_SESSION["loggedin"] = true;
echo "Now logged in!";
}
elseif($_SESSION["loggedin"] == true)
{
 echo "User ist already logged in - &lt;a href=\"".$_SERVER["REQUEST_URI"]."?l=1\"&gt;logout&lt;/a&gt;";
}</pre>
<h4>In MySQL create DB test and execute this query:</h4>
<pre class="prettyprint">CREATE TABLE IF NOT EXISTS `sessions` (
`session_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '',
`session_expires` int(10) unsigned NOT NULL DEFAULT '0',
`session_data` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`IPv4` int(16) unsigned NOT NULL,
`Useragent` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`session_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;</pre>
<p>You can also use InnoDB if you want. Pick wisely because this can affect the performance of your application.</p>
<h3><strong>What does the code do?</strong></h3>
<p>First of all you need to specify a livetime value for your sessions. So if your user clicks on &#8220;Stay logged in&#8221; you will use the 30 day value (see the first lines).Then we initialize our library for the session handling which holds all functions that describe how to behave with sessions.<br />
After everything is initialized, we start the session with a $db which has a resource that leads to our PDO instance.<br />
After you write session_start() you can use the $_SESSION Superglobal variable as you would do normally in PHP at any position in your code. PHP will use our library which saves data to mysql instead of using PHP&#8217;s internal session system.</p>
<p>When executed, you will see a new record in your database:<br />
<a href="http://blog.lukaswojcik.com/wp-content/uploads/2015/10/23-10-2015-10-35-16.png"><img class="alignnone size-full wp-image-29" src="http://blog.lukaswojcik.com/wp-content/uploads/2015/10/23-10-2015-10-35-16.png" alt="23-10-2015 10-35-16" width="728" height="51" /></a></p>
<h3>DEMO</h3>
<p>Try the demo: <a href="http://blog.lukaswojcik.com/demo/sessions.php">DEMO</a></p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.lukaswojcik.com/stay-logged-in-with-php-sessions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>8 Resources to pass the ZCE / ZCPE 5.5 exam successfully</title>
		<link>http://blog.lukaswojcik.com/8-resources-to-pass-the-zce-zcpe-5-5-exam-successfully/</link>
		<comments>http://blog.lukaswojcik.com/8-resources-to-pass-the-zce-zcpe-5-5-exam-successfully/#comments</comments>
		<pubDate>Thu, 18 Jun 2015 12:48:19 +0000</pubDate>
		<dc:creator><![CDATA[Luky]]></dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.lukaswojcik.com/?p=16</guid>
		<description><![CDATA[Do you love PHP as much as I do? So then you want to become a ZCE and have a solid proof, that you can code in this language? Become a Zend Certified Engineer? No wait, Zend recently changed the name of its former certifcate program "ZCE" (for PHP 5.3) ZCPE for PHP 5.5 which means that you will be a Zend Certfied PHP Engineer. Not that it matters, the new ZCPE exam is not really harder to pass than the exams before. It just includes the "new" PHP Features which wern't there in the ZCE exam.]]></description>
				<content:encoded><![CDATA[<p style="text-align: justify;">Do you love PHP as much as I do? So then you want to become a <strong>ZCE </strong>and have a solid proof, that you can code in this language? Become a <strong>Z</strong>end <strong>C</strong>ertified <strong>E</strong>ngineer? No wait, Zend recently changed the name of its former certifcate program &#8220;ZCE&#8221; (for PHP 5.3) <strong>ZCPE</strong> for PHP 5.5 which means that you will be a <strong>Z</strong>end <strong>C</strong>ertfied <strong>P</strong>HP <strong>E</strong>ngineer. Not that it matters, the new ZCPE exam is not really harder to pass than the exams before. It just includes the &#8220;new&#8221; PHP Features which wern&#8217;t there in the ZCE exam.</p>
<p style="text-align: justify;"><span id="more-16"></span></p>
<h2 style="text-align: justify;">The exam</h2>
<p style="text-align: justify;">So what can you expect when attending to the certifcate exam? A lot of PHP Questions which can be sometimes very catchy. There will be 70 single choice, multiple choice and free form questions which you will have to answer in 90 minutes. In addition, when you get multiple choice questions, you won&#8217;t get any hint that there can be one or more correct answers. You will have to know the exact answers, that makes the exam difficult to pass.</p>
<p style="text-align: justify;">I clearly recommend, that you take the exam only, if you have enough experience in PHP and especially in OOP + the newer Features since PHP 5.3. You also have to know about all security risks which are to avoid when developing a Web App with PHP.</p>
<p style="text-align: justify;">I bought a Voucher in May 2013 for the prior ZCE 5.3 exam and had one year to use it. In December 2013 i got the message that there won&#8217;t be any ZCE 5.3 any more and I MUST pass the ZCPE 5.5 exam in order to get my certificate. After a year of preparation, in June 2014 I passed the exam in a Vienna Test center.</p>
<p style="text-align: justify;">And no, I did not prepare myself during the whole year for the exam &#8211; only the three weeks before I finally scheduled the exam date. (Under pressure, thanks Zend for making a 1 year Voucher <span id="result_box" class="short_text" lang="en" tabindex="-1"><span class="hps">validity</span></span>, I probably wouldn&#8217;t take the exam without this :D)</p>
<h2 style="text-align: justify;">Give me the questions11!!!!</h2>
<p style="text-align: justify;">Now after the usual blabla that you can read in every other blog you want to know how I passed the exam, right? I had the same though that you have right now reading other blog author&#8217;s posts: Which resources can help me to <strong>learn everything</strong> for the exam with the <strong>lowest effort</strong>?<br />
Or do you rather think: Stop writing this bulls*** tell me the questions and answers or I gonna &#8220;$*$§%*% with your *§%**§&#8221; and &#8220;*%§%§* and *!&#8221;$§%*%!? (:D)<br />
Sorry, no can do. And you won&#8217;t be able to do this after you pass (or not pass) too, because if you do, you&#8217;ll get banned for livetime from their program. And then all effort was for nothing.</p>
<h2 style="text-align: justify;">1. php.net</h2>
<p style="text-align: justify;">Forget everything else. This will be your bible for the next month. You need to (at least) know the syntax while sleeping and all string / array / file / etc functions that are listed in the documentation. They will ask you which arguments does the function xyz() take. And in which order! And if you are lucky like me, you will learn the proper functions which will be asked.</p>
<p style="text-align: justify;">Click through everything you can find on php.net when preparing for the exam, because it&#8217;s the gold of the gold you can have for your learning phase. Sometimes its also worth to look into the comment section of the manual. They really write useful stuff.</p>
<p style="text-align: justify;"><a href="http://php.net" target="_blank">php.net</a><br />
You can achieve a good start with these topics: <a href="http://php.net/manual/en/book.strings.php" target="_blank">Strings</a>, <a href="http://php.net/manual/en/book.array.php" target="_blank">Arrays</a>, <a href="http://php.net/manual/en/book.pdo.php" target="_blank">PDO,</a> <a href="http://php.net/manual/en/book.stream.php" target="_blank">Streams</a>, <a href="http://php.net/manual/en/features.file-upload.php" target="_blank">File Upload</a>, <a href="http://php.net/spl" target="_blank">SPL</a>, <a href="http://php.net/manual/en/language.namespaces.php" target="_blank">Namespaces</a>, <a href="http://php.net/manual/en/functions.anonymous.php" target="_blank">Lambda</a>, <a href="http://php.net/traits" target="_blank">Traits</a>.</p>
<p style="text-align: justify;">Don&#8217;t forget that mysql_* is dead. (Use mysqli_* or PDO!)</p>
<h2 style="text-align: justify;">2. Zend&#8217;s official FAQ</h2>
<p style="text-align: justify;">Before you begin to study, create a checklist of topics which you don&#8217;t know about and know best.<br />
I opened Excel and created a list of all topics and marked them green, orange and red for the level of skill that I had in these things. I recommend that you do the same.</p>
<p style="text-align: justify;">The best resource for a list of which questions will land on your exam screen can be found here: <a href="http://www.zend.com/en/services/certification/php-5-certification" target="_blank">http://www.zend.com/en/services/certification/php-5-certification</a></p>
<p style="text-align: justify;">Also don&#8217;t miss the FAQ which exactly describes what to do and how to do it: <a href="http://www.zend.com/en/services/certification/faq" target="_blank">http://www.zend.com/en/services/certification/faq</a></p>
<p style="text-align: justify;">I also found this interesting Thread from a user during my exam preparations: <a href="http://devzone.zend.com/1799/passing-the-zce-php-53-certification-exam/" target="_blank">http://devzone.zend.com/1799/passing-the-zce-php-53-certification-exam/</a></p>
<h2 style="text-align: justify;">3. Practice</h2>
<p style="text-align: justify;">I know its not a resource, but studing the php.net manual is not enough. I only passed the exam because I installed XAMPP on my local machine and practiced a lot, although I coded almost 12 years long in PHP. In daily business, you wont come to the &#8220;hidden&#8221; and &#8220;dark&#8221; features that PHP can provide you. And when looking at them i found out that they can be very cool in every day use too.</p>
<p style="text-align: justify;">So go ahead, install a Web server on your machine. But don&#8217;t copy-paste anything from the manual. Write down what you want to do and execute it. And now you likely get a parse error. Pay attention to the exact syntax and you will have a milestone done right ahead.  Practice with different styles of String writing, with the &#8220;, the &#8216;, the HEREDOC and the NOWDOC!</p>
<h2 style="text-align: justify;">4. Official study guide</h2>
<p style="text-align: justify;">The official study guide for ZCE 5.3 from Zend was originally free to download after you purchased a Voucher. They some to offer this now for a charge of $19.95 here: <a href="http://shop.zend.com/en/zend-certification/zend-php-certification-guide-pdf.html" target="_blank">http://shop.zend.com/en/zend-certification/zend-php-certification-guide-pdf.html</a></p>
<p style="text-align: justify;">It was really cool to have that resource because you could see the style of the exam and how they ask questions. I&#8217;m pretty sure that this new guide will offer that kind of style too.</p>
<p style="text-align: justify;">With this guide, you can test your skills and see if you are prepared enough or not. Each topic is good described and you will get questions after every one. The answers are on the bottom of the guide which allow you to compare your results.</p>
<h2 style="text-align: justify;">5. List of Links</h2>
<p style="text-align: justify;">This guy has made an effort to provide you with the links which contain the content to pass the exam: <a href="https://ranawd.wordpress.com/2011/01/17/zend-certified-engineer-zce-study-guide-links/" target="_blank">https://ranawd.wordpress.com/2011/01/17/zend-certified-engineer-zce-study-guide-links/<br />
</a></p>
<p style="text-align: justify;">It&#8217;s definitely worth to stop by and pick some of these links.</p>
<h2 style="text-align: justify;">6. Ultimative guide</h2>
<p style="text-align: justify;">An older, but still useful guide can be found here: <a href="http://zceguide.blogspot.com" target="_blank">http://zceguide.blogspot.com</a></p>
<p style="text-align: justify;">It still contains useful links which can help you come further in your learning process.</p>
<h2 style="text-align: justify;">7. Unofficial study guide</h2>
<p style="text-align: justify;">If you have <span id="result_box" class="short_text" lang="en" tabindex="-1"><span class="hps">knowledge gaps</span></span> with the basic functionality of PHP this might be very handy for you: <a href="http://php-guide.evercodelab.com/" target="_blank">http://php-guide.evercodelab.com/</a></p>
<h2 style="text-align: justify;">8. PHP the right way</h2>
<p style="text-align: justify;">Take some time, preferably 213 hours and go through <a href="http://www.phptherightway.com/" target="_blank">http://www.phptherightway.com/</a> maybe you find something that you did not know before, as I did.</p>
<p style="text-align: justify;">A last tip before you attend to you exam: Don&#8217;t stress, go to the test center, fill out the questions gentle, wait for the 90 minutes to pass.<br />
After the 90 minutes you will get a message box which will allow you to see your result. Now your heart will beat so fast and when you see &#8220;passed&#8221; you will jump out of your seat and shout &#8220;YES&#8221;.<br />
When you study a lot and have a bright experience in PHP, nothing bad will happen.</p>
<p style="text-align: justify;">So as you can see there is a light at the end of the tunnel which can lead you to your beautiful PHP 5.5 certificate. But hey, if you purchase a Voucher now and wait like me for a year, you might be able to take the PHP 7 exam and become a ZCP7E (or something like that).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.lukaswojcik.com/8-resources-to-pass-the-zce-zcpe-5-5-exam-successfully/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Force PHP to show all errors</title>
		<link>http://blog.lukaswojcik.com/force-php-to-show-all-errors/</link>
		<comments>http://blog.lukaswojcik.com/force-php-to-show-all-errors/#comments</comments>
		<pubDate>Sat, 30 May 2015 10:29:43 +0000</pubDate>
		<dc:creator><![CDATA[Luky]]></dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.lukaswojcik.com/?p=5</guid>
		<description><![CDATA[Many times I came across the question why the errors in PHP don't show up and only a blank pages is served. It can be very annoying when developing a huge app and when you want to test something.

Sometimes errors won't even appear when you set all directives to the right values. For example when writing something before "namespace". (Exception the "declare" keyword.)]]></description>
				<content:encoded><![CDATA[<p style="text-align: justify;">Many times I came across the question why the errors in PHP don&#8217;t show up and only a blank pages is served. It can be very annoying when developing a huge app and when you want to test something.</p>
<p style="text-align: justify;">Sometimes errors won&#8217;t even appear when you set all directives to the right values. For example when writing something before &#8220;namespace&#8221;. (Exception the &#8220;<a href="http://php.net/manual/de/language.namespaces.definition.php" target="_blank">declare</a>&#8221; keyword.)</p>
<p style="text-align: justify;"><span id="more-5"></span></p>
<p style="text-align: justify;">PHP has not only runtime options to omit errors.<br />
But in the most cases the following code will bring them back to you:</p>
<pre class="lang-php prettyprint prettyprinted"><code><span class="pln">error_reporting</span><span class="pun">(</span><span class="pln">E_ALL</span><span class="pun">);</span><span class="pln">
ini_set</span><span class="pun">(</span><span class="str">'display_errors'</span><span class="pun">,</span> <span class="lit">1</span><span class="pun">);</span></code></pre>
<p style="text-align: justify;">An other option is to set a .htaccess directive in the folder of your PHP script in which you want to see errors:</p>
<pre class="lang-php prettyprint prettyprinted"><code><span class="pln">php_flag display_errors 0</span></code></pre>
<p style="text-align: justify;">When nothing seems to work, you will need to go into your PHP ini settings and search for the error reporting section:</p>
<p style="text-align: justify;"><em>error_reporting</em><br />
and<br />
<em>display_errors</em></p>
<p style="text-align: justify;">Also, look out for the following code. When it appears, it will override the error_reporting() settings:</p>
<pre class="lang-php prettyprint prettyprinted"><code><span class="pln">set_error_handler();</span></code></pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.lukaswojcik.com/force-php-to-show-all-errors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
