New session on every query Ionic

I got a problem with session GET query on Angular 2, Ionic 2 application. Problem is that I get every time on every query new session when query to server is done. So I can’t to store information in session on server because I got different session each time. Here is my code in Ionic 2:

import { Component } from '@angular/core';

import { NavController } from 'ionic-angular';
import { Http } from "@angular/http";
import 'rxjs/add/operator/toPromise';

@Component({
  selector: 'page-about',
  templateUrl: 'about.html'
})
export class AboutPage {

  constructor(public navCtrl: NavController, public http: Http) {
    let query = this.http.get('http://192.168.1.2/testdb.php');

    query.toPromise().then((result) => {
      console.log(result);
      console.log(result.json());
    });
  }

}

On server I got with code for CORS requests which is told my my session id.

<?php session_start(); //Start session //Headers to disable CORS request: header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Headers: *'); echo "
";
    var_dump(headers_list());       
    echo "
"; echo json_encode(array('success' => true, 'session_id' => session_id())); Here is my header information: ![image](upload://AsOshHfGgendOVQzFNHMQBa5pgE.png) What is can be?

How should your server know that this request is from the same session? In your request you are not sending anything to identify the session, so your server responds with a “Set Cookie: PHPSESSID=…” that you should use with your next requrests. If you include this Cookie, the server will be able to match this and put you in the same session.

(Side note: Maybe think about not using sessions for this but a unique identifier that is generated when your app is started or first installed)

1 Like

Yes. Thank you! It is good solution.

Sujan12Moderator
Feb '17

How should your server know that this request is from the same session? In your request you are not sending anything to identify the session, so your server responds with a “Set Cookie: PHPSESSID=…” that you should use with your next requrests. If you include this Cookie, the server will be able to match this and put you in the same session.

(Side note: Maybe think about not using sessions for this but a unique identifier that is generated when your app is started or first installed)

Hello, I am also having this issue. Do you have any example implementation getting the Cookie from the response?

I could not get the cookie from the response.

return new Promise((resolve, reject) => {
  this.http.post(url, userData, this.config.headers)
    .map(res => {
      console.log(res);
      // I could get the Set-Cookie anywhere
      console.log(res.headers._headersMap.get('Set-Cookie'));

      return res.json();
    })
    .subscribe(data => {
      resolve(data);
    }, error => {
      console.log('Something went wrong signup', error);
      reject(error);
    });
});

Please advice what am I missing. Thanks a lot.

My reply (and others’) to this other question may be of value here. I was able to use $_SESSION to handle user login with ionic-angular-3.9.2. The link:Why session PHP can’t be used with Ionic?