{"id":48,"date":"2014-08-14T22:49:00","date_gmt":"2014-08-14T22:49:00","guid":{"rendered":"https:\/\/www.tech-and-dev.com\/blog\/2014\/08\/14\/phpbb-api-auto-login-a-user-without-a-password\/"},"modified":"2021-02-22T01:02:38","modified_gmt":"2021-02-22T01:02:38","slug":"phpbb-api-auto-login-user-without-password","status":"publish","type":"post","link":"https:\/\/www.tech-and-dev.com\/blog\/2014\/08\/phpbb-api-auto-login-user-without-password.html","title":{"rendered":"phpBB API &#8211; Auto Login a User Without a Password"},"content":{"rendered":"<div dir=\"ltr\" style=\"text-align: left;\">\n<div style=\"clear: both; text-align: center;\"><a style=\"margin-left: 1em; margin-right: 1em;\" href=\"https:\/\/www.tech-and-dev.com\/blog\/wp-content\/uploads\/2021\/02\/phpbb_img.png\"><img decoding=\"async\" title=\"Autologin user in phpBB\" src=\"https:\/\/www.tech-and-dev.com\/blog\/wp-content\/uploads\/2021\/02\/phpbb_img.png\" alt=\"phpBB Logo\" border=\"0\" \/><\/a><\/div>\n<div style=\"text-align: justify;\">In my previous post, <a href=\"http:\/\/www.tech-and-dev.com\/2014\/08\/phpbb-api-logging-in-and-logging-out-a-user.html\" target=\"_blank\" rel=\"noopener\">phpBB API &#8211; Logging In And Logging Out a User<\/a>, I showed how to login a user to phpBB forum using their username and password.<\/div>\n<div style=\"text-align: justify;\"><\/div>\n<div style=\"text-align: justify;\">However in some cases, you may want to login a user without having or knowing their password.<\/div>\n<div style=\"text-align: justify;\"><\/div>\n<div style=\"text-align: justify;\">phpBB API provides a method to auto login users by just knowing their phpBB ID.<\/div>\n<div style=\"text-align: justify;\">This can be useful if you have a website connected to phpBB forum and you&#8217;re only storing the usernames or IDs in that database. Or if you are implementing a Facebook Login Button on your website and you want to synchronize the session with your phpBB forum.<\/div>\n<p>&nbsp;<\/p>\n<blockquote><p><span style=\"font-family: 'Courier New',Courier,monospace;\">&lt;?php<br \/>\ndefine(&#8216;IN_PHPBB&#8217;, true);\/\/Must be defined<br \/>\n$phpbb_root_path = &#8216;..\/phpBB3\/&#8217;;\/\/your forum directory location<br \/>\n$phpEx = substr(strrchr(__FILE__, &#8216;.&#8217;), 1);<br \/>\nrequire_once($phpbb_root_path . &#8216;common.&#8217; . $phpEx);<\/span><\/p>\n<p>function phpbbAutoLogin($id)<br \/>\n{<br \/>\nglobal $phpbb_root_path, $phpEx, $user;<\/p>\n<p>$user-&gt;session_kill(false);\/\/Logout the User &#8211; For testing<br \/>\n$user-&gt;session_begin(); \/\/Start Session<br \/>\n$user-&gt;session_create($id); \/\/Create Session<\/p>\n<p>\/\/Check if User has successfully Logged in<br \/>\nif($user-&gt;data[&#8216;is_registered&#8217;]==1 &amp;&amp; $user-&gt;data[&#8216;user_type&#8217;] != USER_INACTIVE &amp;&amp; $user-&gt;data[&#8216;user_type&#8217;] != USER_IGNORE)<br \/>\n{<br \/>\necho\u00a0 $user-&gt;data[&#8216;username&#8217;] . &#8216; has logged in&#8217;;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\necho &#8216;Error Logging In&#8217;;<br \/>\n}<br \/>\n}<\/p>\n<p>\/\/Auto Login User with phpBB User ID 52<br \/>\nphpbbAutoLogin(52);<br \/>\n?&gt;<\/p><\/blockquote>\n<p>Questions? Please let me know!<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In my previous post, phpBB API &#8211; Logging In And Logging Out a User, I showed how to login a user to phpBB forum using their username and password. However in some cases, you may want to login a user without having or knowing their password. phpBB API provides a method to auto login users [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[63,22,62],"tags":[],"class_list":["post-48","post","type-post","status-publish","format-standard","hentry","category-api","category-php","category-phpbb3"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/posts\/48","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/comments?post=48"}],"version-history":[{"count":1,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/posts\/48\/revisions"}],"predecessor-version":[{"id":268,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/posts\/48\/revisions\/268"}],"wp:attachment":[{"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/media?parent=48"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/categories?post=48"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/tags?post=48"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}