why can''t I session_start() from a PHP CLI application with hashbang?
Results 1 to 7 of 7

Thread: why can''t I session_start() from a PHP CLI application with hashbang?

  1. #1
    Web Interface Architect
    Join Date
    Jan 2002
    Location
    Central Texas
    Posts
    989

    why can''t I session_start() from a PHP CLI application with hashbang?

    for example this:

    --- beginning of page ---
    #!/usr/bin/php -q
    <?php
    session_start();

    it says output/headers already output, cannot send session cookie etc. etc. - you've seen the message before.

    Is it possible I have a \n\r between -q and <?php ?? I have to have the hashbang at the top, so what am I supposed to do here? I can't ob_start() before the hashbang either.

    I'm thinking there is a solution to this - I realize with a CLI executable I really don't "start" a session with a cookie but it would be nice to not change my coding for a bunch of scripts..

    Thanks,
    Sam
    Hey! You may have inadvertently stumbled onto some genius there! :-)

  2. #2
    Senior Member
    Join Date
    Mar 2009
    Posts
    819
    From the manual:

    "To use cookie-based sessions, session_start() must be called before outputing anything to the browser. "
    Declare variables, not war.

  3. #3
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,910
    How are you calling the script? Assuming it's marked executable, you'd just call it by name
    Code:
    $ ./script
    I generally pass the file to the PHP interpreter explicitly, myself (i.e., no shebang).

    Of course, I'm wondering in what context having session data in a command-line script would mean anything anyway: as soon as the script completed that would be the end of the session.
    Last edited by Weedpacket; 11-23-2012 at 04:38 AM.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  4. #4
    Senior Member
    Join Date
    Jul 2007
    Posts
    3,674
    Would it not be easier to simply have two separate ini files and use the web ini file to set the auto_prepend_file directive to a file containing "session_start();"?

    php manual:
    Note:
    To use cookie-based sessions, session_start() must be called before outputing anything to the browser.

    Note:
    This function sends out several HTTP headers depending on the configuration. See session_cache_limiter() to customize these headers.
    php manual:
    A visitor accessing your web site is assigned a unique id, the so-called session id. This is either stored in a cookie on the user side or is propagated in the URL.
    I really doubt you will be able to make sessions work in CLI (even if "work" simply means allow the rest of the script to run).

  5. #5
    Senior Member
    Join Date
    Apr 2003
    Location
    Silver Lake
    Posts
    4,932
    Quote Originally Posted by Weedpacket View Post
    Of course, I'm wondering in what context having session data in a command-line script would mean anything anyway: as soon as the script completed that would be the end of the session.
    Or in what context one would need a shebang in a script hosted by a web server.
    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

  6. #6
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,432
    Quote Originally Posted by sneakyimp View Post
    Or in what context one would need a shebang in a script hosted by a web server.
    That's not much of a mystery, really... reminds me of the good ol' /cgi-bin/. Throw in a script that is to be parsed by any program you like (perhaps even one you create) - it doesn't matter, because the webserver doesn't care. It just executes the script as it would any shell script and passes along the output to the user.

  7. #7
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,729
    Quote Originally Posted by bradgrafelman View Post
    That's not much of a mystery, really... reminds me of the good ol' /cgi-bin/
    NET 1, PERL 2?
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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