Jump to content


Photo

A little Help needed with Headers


  • Please log in to reply
8 replies to this topic

#1 werty37

werty37
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 15 May 2006 - 05:25 AM

<?

/* $Id: Guid.php,v 1.0 2004/07/08 05:50:17 binzy Exp $ */

class System
{
    function currentTimeMillis()
    {
        list($usec, $sec) = explode(" ",microtime());
        return $sec.substr($usec, 2, 3);
    }

}

class NetAddress
{

    var $Name = 'localhost';
    var $IP = '127.0.0.1';
    
    function getLocalHost() // static
    {
        $address = new NetAddress();
        $address->Name = $_ENV["COMPUTERNAME"];
        $address->IP = $_SERVER["SERVER_ADDR"];

        return $address;
    }

    function toString()
    {
        return strtolower($this->Name.'/'.$this->IP);
    }

}

class Random
{
    function nextLong()
    {
        $tmp = rand(0,1)?'-':'';
        return $tmp.rand(1000, 9999).rand(1000, 9999).rand(1000, 9999).rand(100, 999).rand(100, 999);
    }
}


class Guid
{

    var $valueBeforeMD5;
    var $valueAfterMD5;

    function Guid()
    {
        $this->getGuid();
    }

    function getGuid()
    {
        $address = NetAddress::getLocalHost();
        $this->valueBeforeMD5 = $address->toString().':'.System::currentTimeMillis().':'.Random::nextLong();
        $this->valueAfterMD5 = md5($this->valueBeforeMD5);
    }

    function newGuid()
    {
        $Guid = new Guid();
        return $Guid;
    }

    function toString()
    {
        $raw = strtoupper($this->valueAfterMD5);
        return substr($raw,0,8).'-'.substr($raw,8,4).'-'.substr($raw,12,4).'-'.substr($raw,16,4).'-'.substr($raw,20);
    }

}

?>

<?php 

 if (!isset($_COOKIE['SessionID'])): 
   $Guid = new Guid();
   setcookie('SessionID',$Guid->toString());
   echo $_COOKIE['SessionID'];     
 else:
   echo $_COOKIE['SessionID'];
 endif; 

?>


I get an error like this -

Warning: Cannot modify header information - headers already sent

Anyone, help please..

Thanks

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 15 May 2006 - 05:29 AM

Please review [a href=\"http://www.phpfreaks.com/forums/index.php?showtopic=63199\" target=\"_blank\"]http://www.phpfreaks.com/forums/index.php?showtopic=63199[/a]
Legend has it that reading the manual never killed anyone.
My site

#3 werty37

werty37
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 15 May 2006 - 05:34 AM

Thanks for the quick reply...
But the error seems to have tripled...


Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /var/www/test/guid.php:2) in /var/www/test/guid.php on line 3

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /var/www/test/guid.php:2) in /var/www/test/guid.php on line 3

Warning: Cannot modify header information - headers already sent by (output started at /var/www/test/guid.php:2) in /var/www/test/guid.php on line 87

I put "session_start()" at the start of the php file..

<?php session_start();
/* $Id: Guid.php,v 1.0 2004/07/08 05:50:17 binzy Exp $ */

class System
{
    function currentTimeMillis()

............
........
?>

Thanks

#4 jeremywesselman

jeremywesselman
  • Members
  • PipPipPip
  • Advanced Member
  • 154 posts
  • LocationIndependence, KY

Posted 15 May 2006 - 05:39 AM

You can fix this by buffering your output.

<?php
ob_start();
?>

Put this at the very top line of your script.

[!--coloro:#990000--][span style=\"color:#990000\"][!--/coloro--]Jeremy[!--colorc--][/span][!--/colorc--]

#5 werty37

werty37
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 15 May 2006 - 05:47 AM

you mean like this, right

<?php
 ob_start();
 session_start();

/* $Id: Guid.php,v 1.0 2004/07/08 05:50:17 binzy Exp $ */

class System
{
    function currentTimeMillis()
    {
....................................
....................................
....................................
....................................
?>

When i run the page, i get error like this:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /var/www/test/guid.php:2) in /var/www/test/guid.php on line 4

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /var/www/test/guid.php:2) in /var/www/test/guid.php on line 4

Warning: Cannot modify header information - headers already sent by (output started at /var/www/test/guid.php:2) in /var/www/test/guid.php on line 88



Thanks

#6 jeremywesselman

jeremywesselman
  • Members
  • PipPipPip
  • Advanced Member
  • 154 posts
  • LocationIndependence, KY

Posted 15 May 2006 - 06:03 AM

<?php
ob_start();
session_start();
?>

<?php

/* $Id: Guid.php,v 1.0 2004/07/08 05:50:17 binzy Exp $ */

class System
{
    function currentTimeMillis()
    {
....................................
....................................
....................................
....................................
?>

Try it like this.

[!--coloro:#990000--][span style=\"color:#990000\"][!--/coloro--]Jeremy[!--colorc--][/span][!--/colorc--]

#7 werty37

werty37
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 15 May 2006 - 06:11 AM

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /var/www/test/guid.php:2) in /var/www/test/guid.php on line 4

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /var/www/test/guid.php:2) in /var/www/test/guid.php on line 4

Warning: Cannot modify header information - headers already sent by (output started at /var/www/test/guid.php:2) in /var/www/test/guid.php on line 92



Sorry, It didnt help...

Thanks

#8 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 15 May 2006 - 10:18 PM

if you look closer to the error message, it mentions "output started at /var/www/test/guid.php:2". that suggest you might have blank lines before line 2. remove the blank lines and the session will work.

FYI: anything involving headers (header,cookie,session, etc) needs to be sent BEFORE any output.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#9 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 15 May 2006 - 11:13 PM

Read this [a href=\"http://www.phpfreaks.com/forums/index.php?showtopic=37442\" target=\"_blank\"]sticky[/a] in the newbies forum.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users