{"id":13900,"date":"2016-07-14T16:15:11","date_gmt":"2016-07-14T13:15:11","guid":{"rendered":"http:\/\/www.webcodegeeks.com\/?p=13900"},"modified":"2018-01-09T10:49:06","modified_gmt":"2018-01-09T08:49:06","slug":"php-authentication-example","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/php\/php-authentication-example\/","title":{"rendered":"PHP Authentication Example"},"content":{"rendered":"<p>In this example we are going to show you how to add an authentication system to your website. Authentication is an integral part of any web app that works with a login and registration system. Web apps like facebook.com, twitter.com and amazon.com use authentication to secure there users.<\/p>\n<p>Authentication is the process of determining whether someone or something is, in fact, who or what is declared to be. When you login into your facebook account, facebook has to be sure it is really you trying to login to your account and not an impostor. The process by which facebook guarantees that you really own the account (and not your evil twin), is authentication. Authentication has to be done correctly.<br \/>\n&nbsp;<br \/>\n[ulp id=&#8217;8njY7i2QRy6sg8pg&#8217;]<br \/>\n&nbsp;<br \/>\nIn this example we will develop a simple login and registration web app that shows the process of\u00a0authentication in php.<br \/>\nFor this example we will use:<\/p>\n<ol>\n<li>A computer with PHP 5.5 installed<\/li>\n<li>Mysql server<\/li>\n<li>notepad++<\/li>\n<li>Phpmyadmin: This is not compulsory. (All the codes in this example will work correctly even without it). If you want a graphical user interface for manipulating your datbase, then you should install it.<\/li>\n<\/ol>\n<h2>1. Getting Started<\/h2>\n<p>If you are developing on your local machine, you can download and install wamp. Wamp installs both php, phpmyadmin and mysql on your local machine. Furthermore, wamp doesn&#8217;t require any specific prior knowledge to get it working.<\/p>\n<h2>1.1 Initializing the database<\/h2>\n<p>This tutorial assumes you have php and mysql running. You should also create a database named authentication.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>db.php<\/em><\/span><\/p>\n<pre class=\"brush:php; highlight:[6];\"> \r\n&lt;?php\r\n$dsn = \"mysql:dbname=authentication\"; \r\n$username = \"root\"; \r\n$password = \"\"; \r\ntry{\r\n$conn = new PDO( $dsn, $username, $password );  \r\n$conn-&gt;setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);\r\n}\r\ncatch(PDOException $pd){\r\necho $pd-&gt;getMessage();\r\n}\r\n?&gt;\r\n\r\n<\/pre>\n<p>This script creates a connection to the database. We are using PDO (Php data objects) to access our database. The connection is stored in the <code>$conn<\/code> object and it is made available to manipulate the database.<br \/>\nWe create a new PDO object in line 6 and surround it with a try\/catch statement, so as to catch any error that might occur while trying to connect to the database.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>init.php<\/em><\/span><\/p>\n<pre class=\"brush:php; highlight:[2,5];\"> \r\n\r\n&lt;?php\r\nrequire_once(\"db.php\");\r\ntry{\r\n$sql=\"create Table Myuser(id smallint  unsigned not null AUTO_INCREMENT PRIMARY KEY, name VARCHAR(265) NOT NULL,email VARCHAR(265)  NULL,password VARCHAR(265) NOT NULL)\";\r\n$conn-&gt;exec($sql);\r\necho \"TABLE CREATED\";\r\n}\r\ncatch(PDOException $pd){\r\necho \"Error Creating Table: \" . $pd-&gt;getMessage();\r\n}\r\n$conn=null;\/\/close the database connection\r\n?&gt;\r\n\r\n<\/pre>\n<p>Before you register\/login with the index.php script, you should load this script(init.php) into your browser (just once). The script creates a table that is called &#8220;Myuser&#8221; in the database. In line two we include the &#8220;db.php&#8221; script to login to the database. In line 5 we create a table in our database by calling the <code>$conn-&gt;exec($sql)<\/code> method, which takes a string as a parameter (the string holds the sql query).<\/p>\n<h2>1.2 Authenticating users<\/h2>\n<p>Here comes the interesting part. Let&#8217;s take a look at our login and register form.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>index.php<\/em><\/span><\/p>\n<pre class=\"brush:php\"> \r\n&lt;!DOCTYPE html&gt; \r\n&lt;html lang=en&gt;\r\n\t&lt;head&gt;\r\n\t&lt;style&gt;\r\n\thtml, body{\r\n\twidth:100%;\r\n\theight:100%;\r\n\tmargin:0%;\r\n\tfont-family:\"helvetica\",\"verdana\",\"calibri\", \"san serif\";\r\n\toverflow:hidden;\r\n\tpadding:0%;\r\n\tborder:0%;\r\n\t}\r\n\t.a{\r\n\twidth:20%;\r\n\tmargin:10px;\r\n\theight:40px;\r\n\t}\r\n\t.a1{\r\n\tdisplay:inline;\r\n\twidth:20%;\r\n\theight:30px;\r\n\t\r\n\t}\r\n\t#sub{\r\n\tdisplay:inline;\r\n\t\r\n\t\r\n\t}\r\n\r\n\t&lt;\/style&gt;\r\n\t \t\t&lt;meta charset=\"utf-8\" \/&gt;\r\n\t\t&lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi\"\/&gt;\r\n\t\r\n\t&lt;title&gt;&lt;\/title&gt;\r\n\t\r\n\t&lt;\/head&gt;\r\n\t&lt;body&gt;\r\n\r\n\t&lt;h1&gt;Login And Registration Form&lt;\/h1&gt;\r\n\t&lt;div id=cen align=right&gt;\r\n\t\r\n\t&lt;form action=\"submit.php\" method=post&gt;\r\n\t\r\n&lt;input type=text class=a placeholder=name name=name required&gt;&lt;br&gt;\r\n\r\n&lt;input type=email class=a placeholder=email name=email required&gt;&lt;br&gt;\r\n\r\n&lt;input type=password class=a placeholder=Password name=password required&gt;&lt;br&gt;\r\n&lt;input type=submit class=a value=Regster name=register&gt;\r\n\r\n&lt;\/form&gt;\r\n&lt;div align=right&gt;\r\n\\\r\n\t \t&lt;form action=\"submit.php\" method=post&gt;\r\n &lt;input type=email class=a placeholder=email required name=email&gt;&lt;br&gt;\r\n&lt;input type=password class=a placeholder=password required name=password&gt;&lt;br&gt;\r\n&lt;input type=submit class=a value=Login id=sub name=login&gt;\r\n&lt;\/form&gt;\r\n\t\r\n\t&lt;\/div&gt;\r\n&lt;\/div&gt;\r\n\t&lt;\/body&gt;\r\n\t&lt;\/html&gt;\r\n\r\n<\/pre>\n<p>This page contains the login and registration form. After a user fills the login or registration form and submit it, our php script &#8220;submit.php&#8221; is called.\u00a0We created a class called Database in the database.php script. This class contains the methods that perform authentication.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>database.php<\/em><\/span><\/p>\n<pre class=\"brush:php; highlight:[45];\">&lt;?php\r\nrequire_once(\"db.php\");\r\n\r\nclass Database{\r\n\r\npublic function closeDataBase(){\r\n$conn=null;\r\n\r\n}\r\n\/* this checks if the user exists\r\n@param email is users email\r\n@param $conn is the connection object\r\n*\/\r\npublic function checkUserExist($email,$conn){\r\n$sql=\"select count(*) from Myuser where email=:email\";\r\n$st=$conn-&gt;prepare($sql);\r\n$st-&gt;bindValue(':email',$email);\r\n$st-&gt;execute();\r\n$num=$st-&gt;fetchColumn();\r\n$st=null;\r\nif($num&gt;0)\r\nreturn true;\r\nelse \r\nreturn false;\r\n}\r\n\/*\r\nThis method authenticates the user\r\n@param email is users email\r\n@param password is the submitted password\r\n@param $conn is the connection object\r\n*\/\r\npublic function authen($email,$password,$conn){\r\n$sql=\"select password from Myuser where email=:email\";\r\n$st=$conn-&gt;prepare($sql);\r\n$st-&gt;bindValue(\":email\",$email);\r\n$res=$st-&gt;execute();\r\n$row=$st-&gt;fetchAll();\r\nif(password_verify($password,$row[0]['password']))\r\nreturn true;\r\nelse\r\nreturn false;\r\n}\r\n\r\npublic function register($email,$password,$name,$conn){\r\n$hash=password_hash($password,PASSWORD_DEFAULT);\/\/we hash  our password\r\n$sql=\"INSERT INTO Myuser ( email, password,name ) VALUES ( :email, :password ,:name)\"; \r\n$st=$conn-&gt;prepare($sql);\r\n$st-&gt;bindValue(\":email\",$email);\r\n$st-&gt;bindValue(\":password\",$hash);\r\n$st-&gt;bindValue(\":name\",$name);\r\n$st-&gt;execute();\r\n$st=null;\r\n}\r\n} \r\n?&gt;\r\n\r\n<\/pre>\n<p>The class database contains four important functions.<\/p>\n<ul>\n<li><code>closeDataBase(): <\/code> As the name implies it closes the database. It is wise\u00a0to close a database connection after you are through with it. To close a database using PDO you need to destroy the PDO object by ensuring that all remaining\u00a0references to it are deleted\u2014You do this by assigning NULL to the variable that holds the PDO object. If you don&#8217;t do this explicitly, PHP will automatically close the connection when your script ends. We close out database by setting the PDO object to null. <code>$conn=null<\/code> .<br \/>\n<b>Note<\/b>: Many web applications will benefit from persistent connection to the database. Persistent connections will not be closed at the end of the script, but are going to be cached and re-used when another script requests a connection using the same credentials. Persistent connections are useful as it help prevent the overhead of establishing a new connection to the database everytime a script needs to talk to the database.<\/li>\n<li><code>checkUserExist($email,$conn)<\/code> : As the name implies this method checks if a particular user exists. It checks if the email supplied as a parameter exists in the database. If it exists then this user is already registered otherwise\u00a0the user is not registered.<\/li>\n<li><code>register($email,$password,$name,$conn)<\/code> : This code registers a new user. It saves the users email, password and name in the database.<br \/>\n<b>Note:<\/b> Never Never Never! save a users password as text in the database, it is wrong. If your database gets compromised, the attacker will get easy access to your users password(The attacker then will gain access to the users account causing varying degrees of damage). Saving a user password as text, makes your web app vulnerable and defeats the need for authentication.<br \/>\nAlso never use sha1 and md5 to hash your users password. As computing power has increased the feasibility of breaking the SHA1 and md5 hash has been increased. You should use bcrypt for hashing your password. To use bcrypt for password hashing, in line 45 we call the <code>password_hash()<\/code> which takes the password to hash as the first parameter and an hashing algorithm as the second parameter. The algorithm we can use are <code> PASSWORD_DEFAULT<\/code> and \u00a0<code>PASSWORD_BCRYPT <\/code> . If you are going to use the <code>PASSWORD_DEFAULT<\/code> as your hashing algorithm it is recommended to store the result in a database column that can expand beyond 60 characters\u00a0(255 characters would be a good choice).<\/li>\n<li><code> authen() <\/code> This authenticates the user when they try to login. The <code>password_verify()<\/code> function takes a plain password and the hashed string as its two arguments. It returns true if the hash matches the specified password.<\/li>\n<\/ul>\n<p><span style=\"text-decoration: underline;\"><em>submit.php<\/em><\/span><\/p>\n<pre class=\"brush:php; highlight:[3,4,8,20,25];\">&lt;?php\r\nrequire_once(\"database.php\");\r\n$db=new Database();\r\nif(isset($_POST[\"login\"])){\r\n$password=$_POST[\"password\"];\r\n$email=$_POST[\"email\"];\r\nif(!empty($email) &amp;&amp; !empty($password)){\r\nif($db-&gt;checkUserExist($email,$conn)){\/\/if this user exists\r\nif($db-&gt;authen($email,$password,$conn))\r\nheader(\"Location:success.html\");\r\nELSE\r\nheader(\"Location:error.html\");\r\n}\r\nelse \r\nheader(\"Location:error.html\");\r\n\r\n}\r\n$db-&gt;closeDataBase();\r\n}\r\nelse if(isset($_POST[\"register\"])){\r\n$email=$_POST['email'];\r\n$name=$_POST['name'];\r\n$password=$_POST['password'];\r\nif(!empty($email) &amp;&amp; !empty($password) &amp;&amp; !empty($name)){\r\n$db-&gt;register($email,$password,$name,$conn);\r\nheader(\"Location:success.html\");\r\n\r\n}\r\n$db-&gt;closeDataBase();\r\n}\r\n\r\n\r\n?&gt;\r\n\r\n<\/pre>\n<p>In line three we instatiate the database class. In line four we check if the user is trying to login. In line eight we check if the user exists and if the user does not exist or authentication fails we redirect him to an error page. If the authentication succeeds we redirect the user to the welcome page.<\/p>\n<p>In line 20 we check if the user is trying to register. If the user is trying to register, in line 25 we call the <code> $db-&gt;register();<\/code> method to register the user and redirect him to the welcome page<\/p>\n<h2>2. Summary<\/h2>\n<p>In this example we learnt about authentication in php, what it means and how to do it correct. We have also learnt the common pitfalls when implementing authentication and how to\u00a0avoid them.<\/p>\n<h2>3. Download the source code<\/h2>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here: <strong><a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/07\/PhpAuthenticationExample.zip\">PhpAuthenticationExample<\/a><br \/>\n<\/strong><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this example we are going to show you how to add an authentication system to your website. Authentication is an integral part of any web app that works with a login and registration system. Web apps like facebook.com, twitter.com and amazon.com use authentication to secure there users. Authentication is the process of determining whether &hellip;<\/p>\n","protected":false},"author":164,"featured_media":930,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-13900","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>PHP Authentication Example - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"In this example we are going to show you how to add an authentication system to your website. Authentication is an integral part of any web app that works\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.webcodegeeks.com\/php\/php-authentication-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP Authentication Example - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"In this example we are going to show you how to add an authentication system to your website. Authentication is an integral part of any web app that works\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webcodegeeks.com\/php\/php-authentication-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webcodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2016-07-14T13:15:11+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-01-09T08:49:06+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Olayemi Odunayo\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Olayemi Odunayo\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-authentication-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-authentication-example\/\"},\"author\":{\"name\":\"Olayemi Odunayo\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/417918d9b5811210265e8590509718b8\"},\"headline\":\"PHP Authentication Example\",\"datePublished\":\"2016-07-14T13:15:11+00:00\",\"dateModified\":\"2018-01-09T08:49:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-authentication-example\/\"},\"wordCount\":1041,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-authentication-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.webcodegeeks.com\/php\/php-authentication-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-authentication-example\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/php\/php-authentication-example\/\",\"name\":\"PHP Authentication Example - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-authentication-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-authentication-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"datePublished\":\"2016-07-14T13:15:11+00:00\",\"dateModified\":\"2018-01-09T08:49:06+00:00\",\"description\":\"In this example we are going to show you how to add an authentication system to your website. Authentication is an integral part of any web app that works\",\"breadcrumb\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-authentication-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.webcodegeeks.com\/php\/php-authentication-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-authentication-example\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-authentication-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.webcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/php\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PHP Authentication Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"name\":\"Web Code Geeks\",\"description\":\"Web Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.webcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webcodegeeks\",\"https:\/\/x.com\/webcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/417918d9b5811210265e8590509718b8\",\"name\":\"Olayemi Odunayo\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/016b2a353262962fceecf5c274161cd9ef60fdf1593ec95e71d37f5fd9b444f6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/016b2a353262962fceecf5c274161cd9ef60fdf1593ec95e71d37f5fd9b444f6?s=96&d=mm&r=g\",\"caption\":\"Olayemi Odunayo\"},\"description\":\"I am a programmer and web developer, who has experience in developing websites and writing desktop and mobile applications. I have worked with both schematic and schemaless databases. I am also familiar with third party API and working with cloud servers. I do programming on the client side with Java, JavaScript, html, Ajax and CSS while I use PHP for server side programming.\",\"sameAs\":[\"https:\/\/www.webcodegeeks.com\/\"],\"url\":\"https:\/\/www.webcodegeeks.com\/author\/olayemi-odunayo\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP Authentication Example - Web Code Geeks - 2026","description":"In this example we are going to show you how to add an authentication system to your website. Authentication is an integral part of any web app that works","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.webcodegeeks.com\/php\/php-authentication-example\/","og_locale":"en_US","og_type":"article","og_title":"PHP Authentication Example - Web Code Geeks - 2026","og_description":"In this example we are going to show you how to add an authentication system to your website. Authentication is an integral part of any web app that works","og_url":"https:\/\/www.webcodegeeks.com\/php\/php-authentication-example\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_published_time":"2016-07-14T13:15:11+00:00","article_modified_time":"2018-01-09T08:49:06+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","type":"image\/jpeg"}],"author":"Olayemi Odunayo","twitter_card":"summary_large_image","twitter_creator":"@webcodegeeks","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Olayemi Odunayo","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webcodegeeks.com\/php\/php-authentication-example\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-authentication-example\/"},"author":{"name":"Olayemi Odunayo","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/417918d9b5811210265e8590509718b8"},"headline":"PHP Authentication Example","datePublished":"2016-07-14T13:15:11+00:00","dateModified":"2018-01-09T08:49:06+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-authentication-example\/"},"wordCount":1041,"commentCount":0,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-authentication-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","articleSection":["PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.webcodegeeks.com\/php\/php-authentication-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/php\/php-authentication-example\/","url":"https:\/\/www.webcodegeeks.com\/php\/php-authentication-example\/","name":"PHP Authentication Example - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-authentication-example\/#primaryimage"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-authentication-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","datePublished":"2016-07-14T13:15:11+00:00","dateModified":"2018-01-09T08:49:06+00:00","description":"In this example we are going to show you how to add an authentication system to your website. Authentication is an integral part of any web app that works","breadcrumb":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-authentication-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webcodegeeks.com\/php\/php-authentication-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/php\/php-authentication-example\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.webcodegeeks.com\/php\/php-authentication-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"PHP","item":"https:\/\/www.webcodegeeks.com\/category\/php\/"},{"@type":"ListItem","position":3,"name":"PHP Authentication Example"}]},{"@type":"WebSite","@id":"https:\/\/www.webcodegeeks.com\/#website","url":"https:\/\/www.webcodegeeks.com\/","name":"Web Code Geeks","description":"Web Developers Resource Center","publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.webcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webcodegeeks","https:\/\/x.com\/webcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/417918d9b5811210265e8590509718b8","name":"Olayemi Odunayo","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/016b2a353262962fceecf5c274161cd9ef60fdf1593ec95e71d37f5fd9b444f6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/016b2a353262962fceecf5c274161cd9ef60fdf1593ec95e71d37f5fd9b444f6?s=96&d=mm&r=g","caption":"Olayemi Odunayo"},"description":"I am a programmer and web developer, who has experience in developing websites and writing desktop and mobile applications. I have worked with both schematic and schemaless databases. I am also familiar with third party API and working with cloud servers. I do programming on the client side with Java, JavaScript, html, Ajax and CSS while I use PHP for server side programming.","sameAs":["https:\/\/www.webcodegeeks.com\/"],"url":"https:\/\/www.webcodegeeks.com\/author\/olayemi-odunayo\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/13900","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/users\/164"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=13900"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/13900\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/930"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=13900"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=13900"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=13900"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}