How to use session variable from parent folder in a wordpress php code

I have developed a website with login and signup. Inside that website it contains a wordpress blog int he folder “blog”. What i need to check whether the user is logged in or not in the blog. So i can put button like “login”-if not signed in and “dashboard”-if signed in

In parent folder’s php file, i wrote like this

Read More
<?php
session_start();
$_SESSION['username'] = $_POST['username'];
.......
.......
?>

and i used the following code in wordpress blog’s php file, to check whether user have logged and display username.

<?php
session_start();
echo $_SESSION['username'];
....
....
?>

But it gives me and error like this and doesn’t display the username

“Warning: session_start(): Cannot send session cache limiter – headers already sent (output started at C:xampphtdocsit-itechblogwp-includesgeneral-template.php:2343) in C:xampphtdocsit-itechblogwp-contentthemesdazzlingheader.php on line 42”

Related posts

3 comments

  1. this error occurred because the output was sent before the session was started you need to go this page header.php: on line number: 42 where you have included the file named general-template.php and see it does not give an output before the session gets started.

  2. try to use this in you theme’s functions.php

    add_action('init', 'myStartSession', 1);
    add_action('wp_logout', 'myEndSession');
    add_action('wp_login', 'myEndSession');
    
    function myStartSession() {
    if(!session_id()) {
        session_start();
    }
    }
    
    function myEndSession() {
    session_destroy ();
    }
    
  3. You should only be editing your theme files. It sounds like you may have been editing WordPress core files. Your session_start(); should go in the very top of header.php in /wp-content/themes/dazzling/ and ideally with a check…

    if (session_status() == PHP_SESSION_NONE) {
        session_start();
    }
    

    Then you can use your session data – again you should check if its been set (isset etc…) wherever you like in your page templates, which in theory will probably be header.php again.

Comments are closed.