duck
duck

Reputation: 141

php: session works on localhost but server

before posting, I have read:

However, I didn't find a way to solve the problem.

Alright, I am writing an application that uses jQuery AJAX for user login page, to inspect if they are administer, then set a SESSION variable pass into admin.php page. if the SESSION is not set then display an 403 Forbidden page.

I have got the code working on my machine, however, after I deploy all stuff to the server, it always brings up 403 forbidden. I checked SESSION and find it is just Array() with a length of 0;

right now I am trying to do this by:

ini_set('session.save_path', $sessdir); 
session_start();

but I can't get it working on my local machine, here is two sample:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
    
    <?php 
    // abs path of this file
    function inve(){return "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";}
    
    // remove n items from end of a path in string format
    function rewind_url($dire, $ind){
        $pieces = explode('/', $dire);
        for($i=0; $i<$ind; $i++){
            array_pop($pieces);
        }
        $pieces = implode('/', $pieces);
        return $pieces;
    }
    
    function put($str){
        print_r("<h2>" . $str . "</h2>");
    }
    
    $sessdir = rewind_url(inve(), 1);
    ini_set('session.save_path', $sessdir); 
    session_start();
    $_SESSION['name'] = 'joseph';
    
    put($sessdir);
    put(count($_SESSION));

    ?>

    <a href='another.php'>here</a>
    </body>
    </html>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
    
    <?php 
    // abs path of this file
    function inve(){return "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";}
    
    // remove n items from end of a path in string format
    function rewind_url($dire, $ind){
        $pieces = explode('/', $dire);
        for($i=0; $i<$ind; $i++){
            array_pop($pieces);
        }
        $pieces = implode('/', $pieces);
        return $pieces;
    }
    
    function put($str){
        print_r("<h2>" . $str . "</h2>");
    }
    
    $sessdir = rewind_url(inve(), 1);
    ini_set('session.save_path', $sessdir); 
    
    session_start();
    
    put(count($_SESSION));
    
    ?>  
    
    </body>
    </html>

briefly I want to see number 1 after clicking the link. I do appreciate your help.

Upvotes: 0

Views: 486

Answers (1)

Jayr
Jayr

Reputation: 608

Very hard to say, we don't know if even phpsession is enabled for that i should checkout phpinfo();

Further I should check out two things after:

Having all the session_starts(); being at top of every page.

Also, the ini_set('session.save_path', $sessdir); not having full paths with https but relative directory paths explained in the first link you have provided.

Using: $sessdir = dirname(dirname(__FILE__)).'/session_dir'

Upvotes: 2

Related Questions