Nov 8, 2009

Already Sent Session Cookies?

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Scripting > PHP

Already Sent Session Cookies?

lonelym
Er, this is the second topic I've posted about session problems. Here's the html file that keeps showing up:

CODE
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\Program Files\xampp\htdocs\index2\loggingin.php:2) in C:\Program Files\xampp\htdocs\index2\loggingin.php on line 39

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\Program Files\xampp\htdocs\index2\loggingin.php:2) in C:\Program Files\xampp\htdocs\index2\loggingin.php on line 39

Fatal error: Call to undefined function makeform() in C:\Program Files\xampp\htdocs\index2\loggingin.php on line 75


And here's my PHP codes: (Don't worry, I am trying this out offline so there's really no problem)
CODE
<?PHP
// retrieve the submitted values
$username1 = @$HTTP_POST_VARS["username"];
$password1 = @$HTTP_POST_VARS["password"];
$rememberMe = @$HTTP_POST_VARS["rememberMe"];

// make sure that rememberMe has a value
if ($rememberMe == "rememberMe"){
    $rememberMe = "1";
}else{
    $rememberMe = "0";
}

// let the config.php file connect to the database
include("config.php");

// check it the username exist
$query = "Select * from ".$DBprefix."signup where username='$username1'";
$result = mysql_query($query);
if ($row = mysql_fetch_array($result)){
    // check if his account is activated, if not skip to this if's else case
    if ($row["actnum"] == "0"){
        // and check if his account is not loccked, if not skip to this if's else case
        if ($row["numloginfail"] <= 5){
            // finally we check the database to see if the password is correct, if not skip to this if's else case
            if ($row["password"] == $password1){
                // we determin the date for the lastlogin - field.
//
//Custom
//                
if ($row["lastlogin"] == 0){
                $datetime = date("m d, y");
                // and we update that field
                $query = "UPDATE ".$DBprefix."signup Set lastlogin = '$datetime' where username='$username1'";  
                $result = mysql_query($query);
                // now that the correct password is used to log-in, reset the numloginfail-field to 0
                $query = "UPDATE ".$DBprefix."signup Set numloginfail = '0' where username='$username1'";  
                $result = mysql_query($query);
                session_start();
                session_unset();
                session_destroy();
                // put the password in the session
                @ session_register("pass");
                $HTTP_SESSION_VARS["pass"] = $password1;
                // put the username in the session
                @ session_register("id");
                $HTTP_SESSION_VARS["id"] = $username1;
                // send the the cookie if needed
                if($rememberMe=="1"){
                setcookie("rememberCookieUname",$username1,(time()+604800));
                setcookie("rememberCookiePassword",md5($password1),(time()+604800));
                }
                // go to the secured page.
                header("Location: members/learn1.php");
}else{



                $datetime = date("M d,Y");
                // and we update that field
                $query = "UPDATE ".$DBprefix."signup Set lastlogin = '$datetime' where username='$username1'";  
                $result = mysql_query($query);
                // now that the correct password is used to log-in, reset the numloginfail-field to 0
                $query = "UPDATE ".$DBprefix."signup Set numloginfail = '0' where username='$username1'";  
                $result = mysql_query($query);
                // tell we want to work with sessions
                session_start();
                // remove al the data from the session (auto logoff)
                session_unset();
                // remove the session itself
                session_destroy();
                // put the password in the session
                @ session_register("pass");
                $HTTP_SESSION_VARS["pass"] = $password1;
                // put the username in the session
                @ session_register("id");
                $HTTP_SESSION_VARS["id"] = $username1;
                // send the the cookie if needed
                if($rememberMe=="1"){
                setcookie("rememberCookieUname",$username1,(time()+604800));
                setcookie("rememberCookiePassword",md5($password1),(time()+604800));
                }
                // go to the secured page.
                header("Location: members/news.php");
            }}
            else{
                // else the password is incorrect. Therofore we have to update the numloginfield and lastloginfail field
                // first we set $datetime to the current time in a format that we can use to calculate with.
                $datetime = date("d")*10000000000 + date("m")*100000000 + date("Y")*10000 + date("G")*100 + date("i");
                // then we check if the last log-in fail was less than 5 minutes ago.
                if ($row["lastloginfail"] >= ($datetime-5)){
                    // if it is  we update both the numloginfail & the lastloginfail fields.
                    $query = "UPDATE ".$DBprefix."signup Set numloginfail = numloginfail + 1 where username='$username1'";  
                    $result = mysql_query($query);
                    $query = "UPDATE ".$DBprefix."signup Set lastloginfail = '$datetime' where username='$username1'";  
                    $result = mysql_query($query);
                }
                else{
                    // if it is more than 5 minutes ago, just set the lastloginfail field.
                    $query = "UPDATE ".$DBprefix."signup Set lastloginfail = '$datetime' where username='$username1'";  
                    $result = mysql_query($query);
                }
        // and ofcourse we tell the user that his log-in failed.
        makeform($incorrectLogin);}
        }
        // if the numloginfail value is larger than 5 that means there someone tryed to break the password by brute force
        // we will now check how long ago the lock was engaged. it is is more than half an hour ago is, then we will unlock the account
        // and ask the user to login 1 more time to validate it is really him.
        else {
            $datetime = date("d")*10000000000 + date("m")*100000000 + date("Y")*10000 + date("G")*100 + date("i");
            if ($row["lastloginfail"] <= ($datetime-30)){
                // set the numloginfail value to 5 so the user has 1 change to enter his password.
                $query = "UPDATE ".$DBprefix."signup Set numloginfail = '5' where username='$username1'";  
                $result = mysql_query($query);
                // ask the user to enter his username/password once again. Also we set the username field
                // to the name the username entered in the first login of this user. By doing this the makeform function
                // disables the username-field.
                makeform($underAttackReLogin, "$username1");
            }
            else{
            // if it is less than 30 minutes ago ask the user to wait untill the lock is released again.
                echo $underAttackPleaseWait;
            }
        }
    }
    // if the actnum is other than 0 that means the account has not been activated yet.
    else{
    makeform($accountNotActivated);
    }
}
// if the username does not exist we check it is filled in.
else{
    // if it isn't filled we assum that this is the page load and we show the form without an error.
    if ($username1 == ""){    
        makeform("");
    }
    else {
    // if the form is filled it that means that the username does not exist. Therefore we show the form
    // with an error. We can not change the numloginfail or lastloginfail fields for the brute forece attack
    // because the attack isn't pointed at one user.
        makeform($incorrectLogin);
    }
}

// this function shows the form.
// ....m($errormessage="", ... indicates an optionale argument for this function, same for $username.
function makeform($errormessage="", $username2 = ""){

// If you are planning to use A.L.S. for your website, enter the html for your login page below.
// note that that the php codes shouls stay in the place they are now.
// this means (example):
// --your html--
// the place for your errorcode: <?PHP ...(etc) ... ? > (without the space between ? and >)
// -- more html --
// form start, the form actions should be login.php
// the username field (login.php only, other pages: same rules as for other fields):
// <input name="username" type="text" id="username" value=<?PHP ... (etc) ... ? > (without the space between ? and >)
// -- rest of form --
// -- rest of page --
// end of example
// ... (etc) ... indicates the php code between <?PHP and  ? > (without the space between ? and >)
// note: your are allowed to change arguments of the formfields, exept for:
// 'name', 'id', 'type'. al other arguments maybe changed.
?>


This is the logging in PHP file. You are directed here after POSTing your data from the login.php page.

The config file has the DB connection and the username, server type, and password.

I just can't understand what's wrong. Can anyone help?

 

 

 


Comment/Reply (w/o sign-up)

mHelmy
QUOTE(lonelym @ Jun 22 2007, 04:13 PM) *
CODE
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\Program Files\xampp\htdocs\index2\loggingin.php:2) in C:\Program Files\xampp\htdocs\index2\loggingin.php on line 39

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\Program Files\xampp\htdocs\index2\loggingin.php:2) in C:\Program Files\xampp\htdocs\index2\loggingin.php on line 39

Fatal error: Call to undefined function makeform() in C:\Program Files\xampp\htdocs\index2\loggingin.php on line 75


CODE
session_start();
                session_unset();
                session_destroy();
                                ...
                // send the the cookie if needed
                if($rememberMe=="1"){
                setcookie("rememberCookieUname",$username1,(time()+604800));
                setcookie("rememberCookiePassword",md5($password1),(time()+604800));
                }


choose one of them : session or cookies. you cannot use both at the same time because they always set headers.

 

 

 


Comment/Reply (w/o sign-up)

lonelym
I have found out the source of my error. The PHP script should be placed before any text, or else the headers will be sent, and the session functions will not work.

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : Session Cookies

  1. Need Help - How To Remove Session ID From URL - (6)
    Oflate I was going through Google information for webmasters and I noticed the following technical
    guideline for the webmasters: QUOTE Allow search bots to crawl your sites without session IDs or
    arguments that track their path through the site. These techniques are useful for tracking
    individual user behavior, but the access pattern of bots is entirely different. Using these
    techniques may result in incomplete indexing of your site, as bots may not be able to eliminate URLs
    that look different but actually point to the same page. It clearly shows that undesir...
  2. User Authentication Session Handling Problems - Authorization server variables not staying across pages (14)
    This is quite a bit of problem I am facing, and I cannot point exactly where I am going wrong. I
    have been lurking around here at the Asta Host forums with regard to login and user authentication
    scripts and I have got as far as this: - Starting a session - Registering a session variable -
    Using the variable to check if the user is authenticated or not. - Authenticating the user through
    MySQL database - Logging of the user, by setting the session variable to un-authenticated I have
    been able to achive the following things too that I think is not related to this proble...
  3. Php, Sql Lite: Storing Session's Data? - how so store session in SQLITE? (1)
    normally, in windows, session data is saved in the location as directed by the "session.save_path"
    directives. they only show how to store session data in file. is it possible to store it inside the
    SQLite? anyone?...
  4. Run A Script When Expires A Session - (6)
  5. Php Session Problem - (7)
    i have downloaded easyphp on my PC and i am a bit noob with php mysql commands. i have a problem
    making session work the problem that the session file in my server get deleted after leaving the
    page where the session was start for the first time. the problem that the session can only be used
    within the creation page unless you leave it. why?? i have no idea ... i have been looking around
    for three days now .. thank in advance for any help. if you need more details let me know
    /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" />. Joe.k...
  6. Php Forum Cookies - I need help on a php forum cookie. All suggestions welcome. (3)
    OK, I am making a forum in php (with a mysql database). It is much harder then I thought it would be
    but I have been getting along very well. I was just putting some finnishing things on it when I
    found a problem I couldn't figure out. All suggestions are welcome. Problem: I want my forum
    (like all forums) to remember what topics you have already read. I want to do this via cookies and
    at first I was think of having to topic ID to be saved in a string. Example (Cookie:
    %x%) x=the forum topic ID. Then to retrieve that data I would simply look for %x% inside the coo...
  7. How To Delete Files When Session Ends - (4)
    Dear Friends I need solution to a problem. The problem is as under: I am creating certain files
    (playlist) in server disk when user selects some songs. The files are created in ram format. What I
    want to do is to delete these files created during a particular session. Is it possible to do so?
    Now I am deleting these files using on Unload event fired by JavaScript. I am using PHP. ...
  8. Session And Security - (0)
    Hi everyone, I'd like to explain my idea for basic session handling to you guys. I've
    thought about it, and I can't see any problem with it, but I'd like other's opinions as
    well, please. Ok, here goes. So basically, there's two base cases that can happen. User
    visits any page, session class is initialized, etc. Case 1 - User not logged in; no cookie or
    session info in DB If the user is not logged in, has no cookie, and/or there is no session info in
    the DB for this user, any specified activities, protected like so: php: if ( $session...
  9. Sessions, Setting, Unsetting, Reading - and in combination w/ cookies (5)
    I am making a login script which atm uses a cookie to set login status. I would like to include
    sessionwise checking into this. And also an IP check, where i write the IP to database and later get
    it for all other pages and then check it up to the client for each page. I need to know the
    commands for: - getting an IP - Starting a session - Ending a session - Reading a session...
  10. Php And Session - Talk about Session Handling in PHP (4)
    I have a problem in handling Session. When I log on to my site using the passwords and username that
    i have maintained in my database... After authentication, I start a session... but still when i
    again visit the admin page it asks me the username and password... I have added all the
    authentication and checking......



Looking for session, cookies,

See Also,

*SIMILAR VIDEOS*
Searching Video's for session, cookies,
advertisement



Already Sent Session Cookies?

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com