0

I'm using phonegap and I cannot figure out how to start a session.

It's not possible to use php in the code itself because of phonegap. So I need a solution for getting the session by using javascript,

I know that I need to use Ajax but I'm new with ajax and that's most of my problem. So if someone can help me with this I would appreciate it.

I need to load this code in a html page with JavaScript.

<?php

session_start();

if(!empty($_SESSION['login_user']))
{
header('Location: home.php');
}

?>
2

1 Answer 1

2

You cannot. The session is running on the php server, this means only that php server can access the session.

What you can do is load this code from an external page using an iframe or using ajax.

Update:

Use the following code in your app:

jQuery.getJSON("mypage.php", function(data) {
    // data now has all session variables.
});

And on the server side in mypage.php:

<?php
echo json_encode($_SESSION);

This is a simple example, there is no data security here at all.

Sign up to request clarification or add additional context in comments.

5 Comments

depends, what do you want to receive from the php server? Do you want an entire page, or do you just want to receive some data?
just data (the session in this case)
The easiest way to do this is using jQuery. It will allow you to load a json string from the server which you can use in your local javascript.
i sounds easier than it is for me:P
I have added a simple example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.