mohamed.alsum
mohamed.alsum

Reputation: 53

Error when sending GET request using nodejs to apache

i am using nodejs to make request to apache server in certain condition then execute a php function related to this request

GET request in nodejs (server.js)

    var options = {
        host: 'localhost',
        port: 80,
        path: '/myapp/users/logout',
        method: 'GET',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        }
    };

  var req = http.get(options, function(res){
    res.setEncoding('utf8');
    console.log('STATUS: ' + res.statusCode); 
    res.on('data', function(message){
           console.log('Message: ' + message);
        });
    }).on('error', function(e) {
    console.log('ERROR: ' + e.message);});

php function (code igniter) in controllers/users.php

function logout() {
    $this->load->model('user');
    $fb_id = $this->session->userdata('fb_id');
    $player = $this->user->login($fb_id);
    $updated_data = array(
        'isonline' => 0,
        'status' => 'idle',
        'selected' => 0,
        'askedfor_ques' => 0,
        'finish_ques' => 0,
        'is_winner' => 0,
    );
    $this->load->model('cat_sel');
    $room_id = $this->session->userdata('room_id');
    $players_mun = $this->cat_sel->get_numof_players($room_id);
    $updatedroom_data = array(
        'num_of_players' => $players_mun - 1,
    );
    $this->cat_sel->update_room($room_id, $updatedroom_data);
    $this->user->updateuser($player['player_id'], $updated_data);
    $this->session->sess_destroy();
    redirect(base_url() . 'users/login');
}

but when i run this i found there is no values in sessions and get errors shown below in spite of it works fine if i run it without nodejs

Apache log

127.0.0.1 - - [27/Nov/2013:18:55:18 +0200] "GET /myapp/users/logout HTTP/1.1" 302 1261 "-" "-"

nodejs log

STATUS: 302
Message: <div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Undefined offset: 0</p>
<p>Filename: models/cat_sel.php</p>
<p>Line Number: 44</p>

</div><div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Undefined index: player_id</p>
<p>Filename: controllers/users.php</p>
<p>Line Number: 44</p>

</div>

Upvotes: 0

Views: 177

Answers (1)

user3042886
user3042886

Reputation: 26

Node JS and PHP Don't share session data. So by requesting the page using nodejs, your server is unaware who the user is.

Upvotes: 1

Related Questions