{"id":818,"date":"2016-08-06T11:59:24","date_gmt":"2016-08-06T06:29:24","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=818"},"modified":"2026-09-07T16:48:14","modified_gmt":"2026-09-07T11:18:14","slug":"basic-login-and-logout-using-php","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/basic-login-and-logout-using-php\/","title":{"rendered":"How to Implement Basic Login and Logout Using PHP"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">User login and logout functionality is a basic requirement for many web applications. In this article, we will look at a simple implementation of login and logout functionality using PHP sessions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The login process verifies the username and password against the database and stores the authenticated user&#8217;s name in a PHP session. The logout process destroys the session and redirects the user back to the login page.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Before starting, you can also refer to our previous article:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Basic Insert, Delete, Update, Database and Image Upload Using PHP<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before implementing the login and logout functionality, make sure you have:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A PHP development environment.<\/li>\n\n\n\n<li>A MySQL database.<\/li>\n\n\n\n<li>A database table containing user registration details.<\/li>\n\n\n\n<li>Basic knowledge of PHP and MySQL.<\/li>\n\n\n\n<li>Basic understanding of PHP sessions.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Implementation<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. Login Using PHP<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a file named <code>login.php<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The following code connects to the database, starts a PHP session, validates the submitted username and password, and stores the username in the session after successful authentication.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$query=mysql_connect(\"localhost\",\"root\",\"\");\nmysql_select_db(\"demo\",$query);\nsession_start();\nif(isset($_POST['send']))\n{\n$name=$_POST['name'];\n$password=$_POST['password'];\nif($name!=\"\" &amp;&amp; $password!=\"\")\n{ \n$s=mysql_query(\"select *from reg where name = '$name' AND password = '$password'\");\n$a=mysql_fetch_array($s);\n$count=mysql_num_rows($s);\nif($count==0)\n{\necho \"user does not exist\";\n}\nelse\n{\n$_SESSION['name']=$a['name'];\nheader(\"location:indedx.php\");\n}\n}\n}\n&lt;!DOCTYPE html>\n&lt;html>\n&lt;head>\n &lt;title>&lt;\/title>\n&lt;\/head>\n&lt;body>\n&lt;form action=\"\" method=\"post\">\n&lt;table width=\"400\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" >\n &lt;tr>\n &lt;td>username&lt;\/td>\n &lt;td>:&lt;\/td>\n &lt;td>&lt;input type=\"text\" name=\"name\" \/>&lt;\/td>\n \n &lt;\/tr>\n &lt;tr>\n &lt;td>password&lt;\/td>\n &lt;td>:&lt;\/td>\n &lt;td>&lt;input type=\"password\" name=\"password\" \/>&lt;\/td>\n &lt;\/tr>\n &lt;tr>\n &lt;td>&lt;input type=\"submit\" name=\"send\" \/>&lt;\/td>\n &lt;\/tr>\n&lt;\/table>\n&lt;\/form>\n&lt;\/body>\n&lt;\/html><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. Logout Using PHP<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a file named <code>logout.php<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The logout script starts the session, destroys the existing session, and redirects the user to the login page.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php \nsession_start();\nsession_destroy();\nheader(\"location:login.php\");\n?><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. How the Login and Logout Flow Works<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The basic flow is:<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>The user enters their username and password.<\/li>\n\n\n\n<li>PHP checks the submitted credentials against the database.<\/li>\n\n\n\n<li>If the user exists, the username is stored in the PHP session.<\/li>\n\n\n\n<li>The user is redirected to the application page.<\/li>\n\n\n\n<li>When the user selects logout, the PHP session is destroyed.<\/li>\n\n\n\n<li>The user is redirected back to <code>login.php<\/code>.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">PHP sessions provide a simple way to maintain login information between requests. In this basic implementation, the login process verifies the user credentials and stores the username in a session, while the logout process destroys the session and redirects the user to the login page.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently Asked Questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. Why are PHP sessions used for login?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">PHP sessions allow user information to be maintained across multiple page requests after successful authentication.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. How does PHP logout work?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The logout script calls <code>session_destroy()<\/code> to destroy the current session and then redirects the user to the login page.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. Can this login implementation be used with modern PHP?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The concept of using sessions for login and logout is still applicable. However, the <code>mysql_*<\/code> functions used in this original example are no longer supported in modern PHP. Modern implementations should use PDO or MySQLi with prepared statements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related Articles<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/install-apache2-php7-mysql-redhat-7-server\/\">How to Install Apache, PHP, and MySQL on Red Hat 7?<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/how-to-install-mysql-8-0-on-centos-7\/\">How to install MySQL 8.0 on CentOS 7<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Talk to Our Technology Experts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Planning your AWS infrastructure or looking to improve an existing cloud environment? Our team can help with cloud architecture, infrastructure, DevOps, security, deployment, and ongoing optimization.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><a href=\"https:\/\/pheonixsolutions.com\/contact\">Connect with our technology experts.<\/a><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction User login and logout functionality is a basic requirement for many web applications. In this article, we will look [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[],"class_list":["post-818","post","type-post","status-publish","format-standard","hentry","category-web-architecture","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-dc","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/818","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=818"}],"version-history":[{"count":1,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/818\/revisions"}],"predecessor-version":[{"id":11461,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/818\/revisions\/11461"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=818"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}