Member Avatar for feoperro

Hi,

I'm having a problem where I set a session after checking login details, and then change the section respectively via ajax. The problem that I've noticed is that the session is only set once the page is refreshed - I was wondering if it would be possible to set the session via ajax as well?

Thanks,
Ashton.

Member Avatar for diafol

Yes you can certainly set the session via ajax. Just remember to place a session_start() at the head of your script. As session is a cookie, pages can't show it until the server responds (usually page refresh or new page).

Member Avatar for feoperro

When I test the login, I do the following:

1. Login with dummy user/pass
2. Send the input field values to a php file via ajax
3. Set a session = the email address entered
4. Display the session where the login/pass input fields were

However, after clicking login, it gives me a "Notice: Undefined index: " error - But if I press F5 and login again, then it works. So I think the session isn't being passed until the page refreshes. This is why I wanted to find out an alternative way to refreshing the page via ajax.

You have a disconnect somewhere because this certainly is possible to do. I have to agree with ardav, make sure that you have a "session_start()" at the beginning of all of your scripts.

Member Avatar for feoperro

Hi,

Thanks a lot.

At the beginning of all my scripts meaning all my php includes?

...I tried doing this and got this error:

Notice: A session had already been started - ignoring session_start() in

Thanks,
Ashton.

Member Avatar for diafol

I assume you have a login function as opposed to just loose code in an include file. However, if the include file is only ever accessed after an ajax call, it shouldn't really matter. Here's an idea:


1. Call your js script from the onsubmit() or the onclick() call of your form or button. Your choice.
2. The js script send a POST call to your php login file and awaits a response - remember to pass the raw data from the form - let php do the validating/cleaning.
3. Your php login file:

<?php
session_start();
if(!isset($_SESSION['login'])){
  //clean, validate, verify the data - set the session if everything OK
  //you can just echo back a simple response or go json (my fave method) or xml etc 
}else{
  //echo something to say you're already logged in
}
?>

I use jQuery to handle all my ajax calls. An example:

function login(){
  var user = $('#user').val();
  var pass = $('#pass').val();
  $.post("includes/login.inc.php", {"usr": user,"pw": pass},
    function(data){
      $("#login_msg").html(data.msg);
      if(data.logged == '1'){      
        $("#login_area").html(data.logged);
      }  
    },
  "json");
}
Member Avatar for feoperro

Hi,

Thanks again.

I've been messing around with it today and it seems like the problem came where I tried to customize the session.

When I use:

$_SESSION['LoggedInAs']=$_SESSION['LoggedInAs']+1;

Then it doesn't work, but if I use:

$_SESSION['views']=$_SESSION['views']+1;

Then it works...

This isn't really what I want though, I'm trying to set the session to a string, with the users login name. So something like:

$_SESSION['LoggedInAs']="Login@Email.com"

Is it possible?

Thanks,
Ashton.

I've seen people use this technique as a way of casting to int:

$_SESSION['LoggedInAs']=$_SESSION['LoggedInAs']+0;

I am pretty sure that this will do the same thing:

$_SESSION['LoggedInAs']=$_SESSION['LoggedInAs']+1;

I don't think that is what you want because this may very well be just converting your session variable to a number, but I am not sure.

Member Avatar for feoperro

But you know what doesn't make sense... is that, in this link:

How to Use PHP Sessions to Store Data

It says that it IS possible... so I don't know why it's not working for me.

Member Avatar for diafol
$_SESSION['LoggedInAs']="Login@Email.com";

is fine - have you tried it?

Why you're trying to do this...

$_SESSION['LoggedInAs']=$_SESSION['LoggedInAs']+1;

...I can't imagine. This only makes sense if the session var is numeric, in which case you could do:

$_SESSION['LoggedInAs']++;

You may get an error if the session var doesn't yet exist. Check with !isset().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.