need urgent help regarding sessions[solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
punxkista
Forum Newbie
Posts: 3
Joined: Fri Jun 05, 2009 12:47 am

need urgent help regarding sessions[solved]

Post by punxkista »

can anyone please tell me what this (
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\OJT\cart\customer\login.php:2) in C:\xampp\htdocs\OJT\cart\customer\login.php on line 2
:banghead: ) error means, and how i can fix this problem..
Last edited by Benjamin on Sun Jun 07, 2009 11:37 pm, edited 2 times in total.
Reason: Changed bold red text to quote.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: need urgent help regarding sessions.

Post by jaoudestudios »

Session_start() must be sent first, before any other headers! Headers even include white space
punxkista
Forum Newbie
Posts: 3
Joined: Fri Jun 05, 2009 12:47 am

Re: need urgent help regarding sessions.

Post by punxkista »

how can i do that?
here's my code.. I can't trace what's wrong here.. I've done a login system using abbyss x1 but I haven't ecountered problems like this.. I'm using xampp now..

Code: Select all

 
<a href="../index.php">Home</a>
<?php 
[color=#FF0000]session_start(); [/color]
if ($_SESSION['user_info']['userID']!=NULL){
    echo '<script language="javascript">window.location="user.php";</script>';
}
 
?>
<table>
<form action="" method="post">
   <tr>
    <th style="background-color:#FF9900" colspan="2">Login</th>
   </tr>
   <tr>
    <td colspan="2" align="center">
        <?php 
            if (isset($_POST['login'])){
                include '../function/config.php';
                include '../function/db_functions.php';
                
                $user=$_POST[username];
                $pass=$_POST[password];
                
 
                $result=query("SELECT * FROM login WHERE username='$user' AND password='$pass'");
                $row=mysql_num_rows($result);
                if ($row==1){
                    echo 'Login Successful. Redirecting to user page.';
                    $result=query("SELECT * FROM `web-dev`.`user-info` WHERE userID='$user'");
                    [color=#FF0000]$_SESSION['user_info']=mysql_fetch_assoc($result);[/color]
                    echo '<script language="javascript">window.location="user.php";</script>';
                }
                else echo 'Login failed. Username does not exist';
            }
        ?>
    
    </td>
  </tr>
  <tr>
    <td>Username</td>
    <td><input type="text" name="username" /></td>
  </tr>
  <tr>
    <td>Password</td>
    <td><input type="password" name="password"></td>
  </tr>
  <tr>
    <td><a href="./register.php"><small>Register</small></a></td>
    <td><input type="submit" name="login" value="Login" /></td>
  </tr>
 </form>
</table>
Last edited by Benjamin on Sun Jun 07, 2009 11:38 pm, edited 1 time in total.
Reason: Added [code=php] tags.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: need urgent help regarding sessions.

Post by McInfo »

PHP Manual wrote:Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.
This also applies to session_start().

Your problem is here:

Code: Select all

<a href="../index.php">Home</a>
<?php
session_start(); 
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 10:14 pm, edited 1 time in total.
punxkista
Forum Newbie
Posts: 3
Joined: Fri Jun 05, 2009 12:47 am

Re: need urgent help regarding sessions[solved].

Post by punxkista »

thanksss a lot jaoudestudios and mcinfo.. I figured it out now..so thats the reason why tutorials advice to put it at the very top of the page..your're a great help..i really appreciate it.. :D
Post Reply