SitePoint Sponsor

User Tag List

Results 1 to 23 of 23
  1. #1
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP session and ajax

    Hi guys,

    Once again i have come to halt in my coding with a problem. I am making a php/ajax website. After generating the initial html and javascript code with php, i have a link which communicates with another php script using xmlhttprequest.

    This works great. I am using post by the way, not get. Once i call the php script, i want it to be able to have access to the session variables. So, i run, session_start at the very top. Php has a problem with this saying i have already sent headers (i am assuming this is in reference to the headers that must be sent in order for post to work?). Is there a way around this or should i change to get? I would like to use post as it is more secure and also there is no limit to the character length im able to pass.

    Heres the function calling the php script using post.

    Code:
    function toggle_week(element_id)
    {
      clear_slate();
      document.getElementById("statusbar").innerHTML = "Loading...";
    
      var myscript = "toggle_week.php";
      var params = "direction="+element_id;
      http.open("POST", myscript, true);
    
      http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http.setRequestHeader("Content-length", params.length);
      http.setRequestHeader("Connection", "close");
    
      http.onreadystatechange = function(){
        if(http.readyState == 4 && http.status == 200) {
          
          document.getElementById("statusbar").innerHTML = "done"; //Remove this later
          var info = (http.responseText).split("|");
          change_dates(info[0]);     
    
    	}
      }
      http.send(params);
    }
    Any help or pointing out of flaws in my logic would be greatly appreciated!!!

  2. #2
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What you posted is javascript and has nothing to do with that php error which is being caused by sending output or whitespace before calling session start.

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    check your php file containing the session_start, there must not be an empty line before <?php (this is a common error) if there's an empty line it will be sent first which won't work with session ...

    btw why don't you use some javascript frameworks like jquery which is quiet easy to handle?

  4. #4
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I realise that i posted javascript, but i *think* my php code cannot be the issue (as i have sucessfully got sessions going before). But here is the php script if i missed something:

    Code:
    <?
      session_start();
    
      require('../lib/defs');
      include_once('../home/helper.php');
      include_once('../home/timesheetfx.php');
    
      $direction = $_POST['direction'];
      $start_date = $_SESSION['startdate'];
      $end_date = $_SESSION['enddate'];
    
      $points = $middle_point($start_date, $end_date, false);
    
      if($direction == "right")
      {
        $start_date = $points[0];
      }
      else
      {
        $end_date = date(strtotime("-1 day", $points[0]));
      }
    
      $output  = $start_date."|";
      $output .= appointment_retrieve($_SESSION['timesheet']);
    
      echo $output;
    ?>

    There is no line between the <?php line and the first line. Let me just quickly say all the included files are just helper functions, that do not have calls to start sessions.

    As for the jQuery stuff, well i really should, but i am just learning this stuff. So i think its probably in my best interest to learn it from first princples. Would'nt you agree?

  5. #5
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It must be the PHP - Javascript doesn't output errors.

    Can you copy/paste the error, so we know the line, file, etc.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  6. #6
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What is before the opening <? tag?

  7. #7
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The error:

    Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /export/grieg/1/pwee167/apache1333/htdocs/webtms/home/toggle_week.php:1) in /export/grieg/1/pwee167/apache1333/htdocs/webtms/home/toggle_week.php
    on line 2.

    Line 2 is the call to session_start(). There is nothing before it. So how can it be the php?

  8. #8
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is the file you posted above called toggle_week.php?

  9. #9
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is nothing before the <? tag. Have any of you guys used post with the xmlhttprequest to a php script and tried to retain session variables?

  10. #10
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pwee167 View Post
    There is nothing before the <? tag. Have any of you guys used post with the xmlhttprequest to a php script and tried to retain session variables?
    Yes!

  11. #11
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeh the php code above is toggle_week.php

  12. #12
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    have you tried removing the session_start() and seeing what happens?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  13. #13
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Found an error i accidently put a $ before the middle_point function! Maybe this is it!

  14. #14
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well there is nothing wrong in that code so my guess is it is a BOM mark. Can you post a URL to the page?

  15. #15
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://pwee167.srvr.cse.unsw.edu.au/

    goto the webtms folder.
    login: prabu
    pwd: prabu

  16. #16
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Btw: the the link is the "<<" arrows to toggle weeks.

    and what is a BOM mark?

  17. #17
    SitePoint Zealot
    Join Date
    Dec 2005
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Make sure you don't have a blank line before the first "<?php" line. For example, do NOT have:

    Code:
         
    <?php
    But instead, have:

    Code:
    <?php
    If you have an empty line at the top before "<?php", it gets outputted to the browser. Hope that helps!

    PS. And yeah, I don't understand the "$middle_point($start_date, $end_date, false);" thing either. I would imagine you're getting a parse error from that as well.
    Kiopa Software -- Demo Now Online! Check it out!
    Goal: Consolidate all data & tools you use on a daily basis.
    Grand opening special, licenses FREE for a limited time.

  18. #18
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  19. #19
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by RDFrame View Post
    Make sure you don't have a blank line before the first "<?php" line. For example, do NOT have:

    Code:
         
    <?php
    But instead, have:

    Code:
    <?php
    If you have an empty line at the top before "<?php", it gets outputted to the browser. Hope that helps!

    PS. And yeah, I don't understand the "$middle_point($start_date, $end_date, false);" thing either. I would imagine you're getting a parse error from that as well.

    Yep i checked that already, does not seem to be the culprit. I have resolved the $middle_point error, and the issue seems to still be there.

  20. #20
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    What file encrytion are you using? UTF-8 has 3 invisible characters on the front of the file, which PHP counts as outputted text.

    Try using ANSI encryption.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  21. #21
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Post a proper Link that links to toggle_week.php

  22. #22
    SitePoint Zealot
    Join Date
    Dec 2005
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Invisible characters maybe? You using a MAC, or a weird text editor by chance? If you're on a LINUX box, upload the file, login via SSH, and at the prompt type:

    Code:
    dos2unix toggle_week.php
    Should clear up anything needed.
    Kiopa Software -- Demo Now Online! Check it out!
    Goal: Consolidate all data & tools you use on a daily basis.
    Grand opening special, licenses FREE for a limited time.

  23. #23
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Problem solved! There was a mistake with converting a timestamp to a date! Thanks for all the help guys!


Bookmarks

Posting Permissions

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