Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jul 2002
    Location
    Sweden
    Posts
    66
    Thanks
    1
    Thanked 0 Times in 0 Posts

    PHP and Ajax problems (cookie and session)

    Hi.

    I have 2 problems I would like to get answered.
    Neither needs any "real" code displayed from my side, since it's not therein the problems lies.
    Note that this is also simplified, to better demonstrate what I mean.

    1.
    I have a page where when a link is pressed ajax is called and it eventually runs a php function.
    This function does 2 things. First sets a cookie, then echo's stuff into a predefined layer.

    PHP Code:
    function setUsername($name)
    {
    setcookie("uname"$name0"/");
    echo 
    "Username: " $_COOKIE["uname"];

    The problem is that the cookie doesn't exist until ajax is done.
    The above code would give an error saying that cookie doesn't not exist.
    I've tried sleep etc, but it seems to depend on if ajax has finished.
    Why is that so and how can I circumvent this?

    2.
    Are there any good ways to combine sessions and ajax?
    I've tried http://codinginparadise.org/weblog/2...on-across.html but it has flaws.
    I can get neither example to work in safari and when the techniques are used in FF/IE,
    they only work in a .htm/.html file. If I try them in .php the field/iframe is reset on refresh.

    Thanks in advance!
    Last edited by Lazaroth; 07-06-2008 at 01:35 AM.

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    as you've already realized the cookie value becomes available only after the completion of the ajax request. this is because cookies are set on the browser using http headers so, they must reach the browser to be set and sleeping on the server-side isn't going to help.

    now, one way to do it would be to simply set the cookie in the ajax call and when the call completes read the uname value from the cookie using javascript
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • Users who have thanked shyam for this post:

    Lazaroth (07-07-2008)

  • #3
    New Coder
    Join Date
    Jul 2002
    Location
    Sweden
    Posts
    66
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ah, so that is how it works.

    Your suggestion is simple yet brilliant, I'll try something like that later tonight!

  • #4
    New Coder
    Join Date
    Jul 2002
    Location
    Sweden
    Posts
    66
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Works like a charm!

    Now if I only could get sessions to work aswell...

  • #5
    Regular Coder
    Join Date
    Nov 2007
    Location
    Leeds, UK
    Posts
    514
    Thanks
    24
    Thanked 19 Times in 19 Posts
    Same way mate
    Ajax -> php $_SESSION['name'] = $value
    AJAX Complete

    AJAX -> php echo $_SESSION['name'] - and get javascript to save it as var and there you go
    AJAX Complete

  • #6
    New Coder
    Join Date
    Jul 2002
    Location
    Sweden
    Posts
    66
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I think I know how you mean, although I'm not sure how to do it.

    First, through ajax set session = some value
    Code:
    Ajax -> php $_SESSION['name'] = $value
    AJAX Complete
    Secondly, though ajax get the session and send to javascript so everything can be read.
    Code:
    AJAX -> php echo $_SESSION['name'] - and get javascript to save it as var and there you go
    AJAX Complete
    The thing I don't understand is since session is set though ajax, the session isn't really set. And that is the problem with using ajax and sessions.
    So if I try to get it in an other call, it will show session undefined.
    Have I misunderstood how ajax works or am I missing something else?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •