Results 1 to 4 of 4

Thread: My SQL Session start fail...

  1. #1
    Join Date
    Oct 2007
    Posts
    53
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default My SQL Session start fail...

    Hey, I have here a small amount of code to gather the # of active users... The script itself is fine (I think), but the session_start is giving me issues... Any help would be great!
    PHP 5.2.*
    MySQL 5.*
    <passwordhere> is the actual pass on the normal script of course .
    (problem below)
    Code:
    Code:
    <?php
    mysql_connect('localhost', 'shadowte_shadow', '<passwordhere>');
    mysql_select_db('shadowte_site');
    session_start();
    function update_sessions()
    {
        $sid = session_id();
        if($_SESSION['sitename_online'] == "1")
        {
            mysql_query("UPDATE `shadow_sessions` SET `time` = '". time() ."' WHERE `sid` = '$sid'") or die(mysql_error());
        }
        else
        {
            $_SESSION['sitename_online'] = 1;
            mysql_query("INSERT INTO `shadow_sessions` SET `time` = '". time() ."', `sid` = '$sid'") or die(mysql_error());
        }
    }
    function get_onlineusers()
    {
        $min = time() - 301;
        mysql_query("DELETE FROM `shadow_sessions` WHERE `time` <= '$min'") or die(mysql_error());
        $query = mysql_query("SELECT COUNT(sid) FROM `shadow_sessions`");
        $num = mysql_fetch_row($query);
        return($num[0]);
    }
    update_sessions();
    echo "There are currently ". get_onlineusers() ." users online.";
    ?>
    error received:
    Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/shadowte/public_html/shadowtechstudios.net/php/usercount.php:9) in /home/shadowte/public_html/shadowtechstudios.net/php/usercount.php on line 10

    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/shadowte/public_html/shadowtechstudios.net/php/usercount.php:9) in /home/shadowte/public_html/shadowtechstudios.net/php/usercount.php on line 10
    No database selected

    if you would like to see it yourself:
    http://php.shadowtechstudios.net/usercount.php

    Thanks again.

    Update: after removing the html formating I do not recieve the php errors, however I do still get the no database selected... can someone tell me why it doesn't see what database i chose?
    Last edited by AmenKa; 04-30-2008 at 04:58 AM.

  2. #2
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default

    session_start(); has got to be the first thing on your page or it won't work properly!

  3. #3
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    actually second... the <?php needs to be first or the session wont be initiated in the first place... but you made the assumption it was there anyway

  4. #4
    Join Date
    Oct 2007
    Posts
    53
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    I moved session start two lines up (directly below <?php), I get the same thing: No database selected.:
    Code:
    <?php
    session_start();
    mysql_connect('localhost', 'shadowte_shadow', '<pass>');
    mysql_select_db('shadowte_site');
    function update_sessions()
    {
        $sid = session_id();
        if($_SESSION['sitename_online'] == "1")
        {
            mysql_query("UPDATE `shadow_sessions` SET `time` = '". time() ."' WHERE `sid` = '$sid'") or die(mysql_error());
        }
        else
        {
            $_SESSION['sitename_online'] = 1;
            mysql_query("INSERT INTO `shadow_sessions` SET `time` = '". time() ."', `sid` = '$sid'") or die(mysql_error());
        }
    }
    function get_onlineusers()
    {
        $min = time() - 301;
        mysql_query("DELETE FROM `shadow_sessions` WHERE `time` <= '$min'") or die(mysql_error());
        $query = mysql_query("SELECT COUNT(sid) FROM `shadow_sessions`");
        $num = mysql_fetch_row($query);
        return($num[0]);
    }
    update_sessions();
    echo "There are currently ". get_onlineusers() ." users online.";
    ?>
    Last edited by AmenKa; 05-01-2008 at 01:18 AM.

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
  •