{"id":22156,"date":"2011-04-11T21:26:38","date_gmt":"2011-04-11T15:56:38","guid":{"rendered":"https:\/\/www.amitbhawani.com\/blog\/?p=22156"},"modified":"2011-04-11T21:28:23","modified_gmt":"2011-04-11T15:58:23","slug":"sessions-wordpress-storing-user-data","status":"publish","type":"post","link":"https:\/\/www.amitbhawani.com\/blog\/sessions-wordpress-storing-user-data\/","title":{"rendered":"How to Use Sessions in WordPress For Storing User Data?"},"content":{"rendered":"<p>It is surprising that WordPress doesn\u2019t use Sessions for storing data. It is a stateless application. So that if you want to use sessions in your custom modifications or plugins of WordPress, you just need to follow the below ideas!<\/p>\n<p><strong>Idea 1:<\/strong><br \/>\nGo to the root of your blog (where WordPress is installed). Normally upgrading your WordPress installation will replace all files, except wp-config.php. Edit wp-config.php file and add the following line at the beginning of the file:<\/p>\n<p><code>\/\/ call to sessions in WordPress.<br \/>\n>if (!session_id())<br \/>\nsession_start(); <\/code><\/p>\n<p>Now you can use sessions in WordPress. For most plugins, you can pass variables in URLs and hidden fields or use cookies, instead of using sessions to store data in WordPress.<\/p>\n<p><strong>Idea 2 :<\/strong><\/p>\n<p>Here is 2nd idea to use sessions in WordPress. Using the below hook is much better for plugin and themes so you don\u2019t need to worry about hacking the root files or modify wp-config.php file. A simple function you can add in your functions.php file, which will start a session by itself. If you use this, then you don\u2019t need the above session_start(); code. <\/p>\n<p><code>function cp_admin_init() {<br \/>\nif (!session_id())<br \/>\nsession_start();<br \/>\n}<\/p>\n<p>add_action(\u2018init\u2019, \u2018cp_admin_init\u2019);<br \/>\n<\/code><\/p>\n<p><strong>Idea 3 :<\/strong><\/p>\n<p>If both the above codes are not working for you, here is the 3rd idea. Enter these codes in functions.php<\/p>\n<p><code>function login_to_wp($u_name){<br \/>\nrequire(\u2018wp-blog-header.php\u2019);<br \/>\n$user_login = $u_name;<br \/>\n$user = get_userdatabylogin($user_login);<br \/>\n$user_id = $user->ID;<br \/>\nwp_set_current_user($user_id, $user_login);<br \/>\nwp_set_auth_cookie($user_id);<br \/>\ndo_action(\u2018wp_login\u2019, $user_login);<br \/>\n}<\/p>\n<p>function logout_to_wp($u_name){<br \/>\nrequire(\u2018wp-blog-header.php\u2019);<br \/>\n$user_login = $u_name;<br \/>\n$user = get_userdatabylogin($user_login);<br \/>\n$user_id = $user->ID;<br \/>\nwp_set_current_user($user_id, $user_login);<br \/>\nwp_set_auth_cookie($user_id);<br \/>\ndo_action(\u2018wp_logout\u2019, \u201d);<br \/>\n}<\/p>\n<p>login_to_wp($u_name);<br \/>\n}else{<br \/>\nlogout_to_wp(\u201d);<br \/>\n} <\/code><\/p>\n<p>If your server is currently running register_globals on, you should modify a function named wp_unregister_GLOBALS , which can be found in your wp-settings.php file located in the root directory (where wp-cofig.php file located). To allow sessions you simply have to insert _SESSION into the array for the code, which is located around line 39: <\/p>\n<p><code>$noUnset = array(\u2018GLOBALS\u2019, \u2018_GET\u2019, \u2018_POST\u2019, \u2018_COOKIE\u2019, \u2018_REQUEST\u2019, \u2018_SERVER\u2019, \u2018_ENV\u2019, \u2018_FILES\u2019, \u2018table_prefix\u2019); <\/code><\/p>\n<p>Then the final code will look like below: <\/p>\n<p><code>$noUnset = array(\u2018_SESSION\u2019,'GLOBALS\u2019, \u2018_GET\u2019, \u2018_POST\u2019, \u2018_COOKIE\u2019, \u2018_REQUEST\u2019, \u2018_SERVER\u2019, \u2018_ENV\u2019, \u2018_FILES\u2019, \u2018table_prefix\u2019); <\/code><\/p>\n<p>Hope this will solve your sessions problem in WordPress.<\/p>\n<p><strong>Guest Writer :<\/strong> Musthafa Ullal has a MCSE 2000 &#038; 2003 with specialization in Messaging, Technology Geek and founder &#8211; blogger in TekNoise.com. Visit TekNoise Blog <a href=\"http:\/\/teknoise.com\/\">here<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>It is surprising that WordPress doesn\u2019t use Sessions for storing data. It is a stateless application. So that if you want to use sessions in your custom modifications or plugins of WordPress, you just need to follow the below ideas! Idea 1: Go to the root of your blog (where WordPress is installed). Normally upgrading [&hellip;]<\/p>\n","protected":false},"author":32,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[376],"tags":[],"class_list":["post-22156","post","type-post","status-publish","format-standard","category-wordpress","entry"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":3757,"url":"https:\/\/www.amitbhawani.com\/blog\/move-wordpress-blog\/","url_meta":{"origin":22156,"position":0},"title":"How to Transfer a WordPress Blog from One Host to Another","author":"Amit Bhawani","date":"May 11, 2009","format":false,"excerpt":"Do you want to Transfer a Wordpress based blog from one web hosting company to another one? Then you might find this tutorial helpful because we are going to actually transfer a website and recommend you the most important steps which should be taken care of while moving the website.\u2026","rel":"","context":"In &quot;How to Guide&quot;","block_context":{"text":"How to Guide","link":"https:\/\/www.amitbhawani.com\/blog\/category\/how-to\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":6376,"url":"https:\/\/www.amitbhawani.com\/blog\/wordpress-blog-backup-plugins-tools-tutorials\/","url_meta":{"origin":22156,"position":1},"title":"WordPress Blog Backup Plugins Tools &#038; Tutorials","author":"Amit Bhawani","date":"October 12, 2009","format":false,"excerpt":"You have been blogging from a long period and you have been making thousands of dollars from your blog but what about the security of your this money making machine? Iam not taking about any other security but the important content you have written everyday, the thousands of comments you\u2026","rel":"","context":"In &quot;Wordpress Guide&quot;","block_context":{"text":"Wordpress Guide","link":"https:\/\/www.amitbhawani.com\/blog\/category\/wordpress\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.amitbhawani.com\/blog\/wp-content\/uploads\/2010\/12\/Wordpress-Backup-Tools-Plugins.jpg?fit=500%2C275&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":10348,"url":"https:\/\/www.amitbhawani.com\/blog\/how-to-sign-out-gmail-account-from-another-computer\/","url_meta":{"origin":22156,"position":2},"title":"How to Sign Out Gmail Account from another Computer","author":"Tony Stark","date":"April 21, 2010","format":false,"excerpt":"If you have accessed your Gmail account from a friends computer, or a Internet Center, and have forgot to logout from there, then your account can be used by anyone who has access to that PC, which is when you should lot out from that PC asap and for this\u2026","rel":"","context":"In &quot;Security Blog&quot;","block_context":{"text":"Security Blog","link":"https:\/\/www.amitbhawani.com\/blog\/category\/security\/"},"img":{"alt_text":"Gmail Account Activity","src":"https:\/\/i0.wp.com\/www.amitbhawani.com\/blog\/wp-content\/uploads\/2010\/04\/Gmail-Account-Activity.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":12661,"url":"https:\/\/www.amitbhawani.com\/blog\/setup-wordpress3-multi-user-blog\/","url_meta":{"origin":22156,"position":3},"title":"How to Setup WordPress 3 Multi User Blog &#8211; Mu Tutorial","author":"Amit Bhawani","date":"July 15, 2010","format":false,"excerpt":"Wordpress 3.0 offers with the Mu system inbuilt and there is no need to download and install the wordpress mu script because you just need to set some settings and the blog is ready. Just upgrade your current Wordpress blog to the latest version and then make sure you are\u2026","rel":"","context":"In &quot;Wordpress Guide&quot;","block_context":{"text":"Wordpress Guide","link":"https:\/\/www.amitbhawani.com\/blog\/category\/wordpress\/"},"img":{"alt_text":"Wordpress Mu Network Site","src":"https:\/\/i0.wp.com\/www.amitbhawani.com\/blog\/wp-content\/uploads\/2010\/07\/Wordpress-Mu-Network-Site.png?fit=560%2C260&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.amitbhawani.com\/blog\/wp-content\/uploads\/2010\/07\/Wordpress-Mu-Network-Site.png?fit=560%2C260&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.amitbhawani.com\/blog\/wp-content\/uploads\/2010\/07\/Wordpress-Mu-Network-Site.png?fit=560%2C260&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":433,"url":"https:\/\/www.amitbhawani.com\/blog\/top-tips-tweaks-reduce-firefox-memory-usage\/","url_meta":{"origin":22156,"position":4},"title":"5 Top Tips\/Tweaks to Reduce Firefox Memory Usage","author":"Amit Bhawani","date":"April 28, 2008","format":false,"excerpt":"Firefox Browser is generally considered as high resource using application but thats not the point always, since most applications usage similar resources when used for longer period and since this is a Browser application it needs to be running for a longer period and this increases its memory consumption. If\u2026","rel":"","context":"In &quot;Browsers Blog&quot;","block_context":{"text":"Browsers Blog","link":"https:\/\/www.amitbhawani.com\/blog\/category\/browsers\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.amitbhawani.com\/blog\/wp-content\/uploads\/2010\/12\/windows-task-manager.jpg?fit=405%2C557&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":1482,"url":"https:\/\/www.amitbhawani.com\/blog\/backup-blogs-email-computer\/","url_meta":{"origin":22156,"position":5},"title":"How to Schedule Backup Blogs to Email\/Computer","author":"Amit Bhawani","date":"September 25, 2008","format":false,"excerpt":"Its very good that you have started blogging and have started making money online but taking a backup of all your data is very important as much as is the promotion of your blog. Generally most people neglect this part when they are into serious blogging and when problems like\u2026","rel":"","context":"In &quot;How to Guide&quot;","block_context":{"text":"How to Guide","link":"https:\/\/www.amitbhawani.com\/blog\/category\/how-to\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.amitbhawani.com\/blog\/wp-json\/wp\/v2\/posts\/22156","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.amitbhawani.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.amitbhawani.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.amitbhawani.com\/blog\/wp-json\/wp\/v2\/users\/32"}],"replies":[{"embeddable":true,"href":"https:\/\/www.amitbhawani.com\/blog\/wp-json\/wp\/v2\/comments?post=22156"}],"version-history":[{"count":0,"href":"https:\/\/www.amitbhawani.com\/blog\/wp-json\/wp\/v2\/posts\/22156\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.amitbhawani.com\/blog\/wp-json\/wp\/v2\/media?parent=22156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amitbhawani.com\/blog\/wp-json\/wp\/v2\/categories?post=22156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amitbhawani.com\/blog\/wp-json\/wp\/v2\/tags?post=22156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}