Simple Login problem

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
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Simple Login problem

Post by klevis miho »

I'm doing a login with sessions. I created a database, with id, username and password.

Code: Select all

<?php 
    include('../includes/connection.php');
 
    $user = addslashes($_POST['username']);
    $pass = md5($_POST['password']);
    
    $result = mysql_query("SELECT * FROM perdorues WHERE username='$user' AND password='$pass'");
    
    $rowCheck = mysql_num_rows($result);
    if($rowCheck > 0) {
        while($row = mysql_fetch_array($result)) {
            session_start();
            session_register('username');
            echo 'Success';
        }
    } else {
        echo 'Incorrect login name or password';
    }
?>

And it displays me that error:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\fjalor\includes\connection.php:6) in C:\xampp\htdocs\fjalor\admin\login.php on line 12
Success


Any help would be appreciated
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Simple Login problem

Post by jackpf »

Connection.php is outputting something. Put session_start() at the top of the script.

Also, since it's in a while loop, if multiple rows are returned, you'll be calling session_start() multiple times...which you'r enot supposed to do.
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Simple Login problem

Post by klevis miho »

Here the code again with the connection included, i also tried putting session_start() on the top:

Code: Select all

<?php
$connection = mysql_connect('localhost','root','') or die(mysql_error());
$selectDB = mysql_select_db('fjalorinformatike') or die(mysql_error());
 
    $user = addslashes($_POST['username']);
     $pass = md5($_POST['password']);
   
     $result = mysql_query("SELECT * FROM perdorues WHERE username='$user' AND password='$pass'");
    
     $rowCheck = mysql_num_rows($result);
     if($rowCheck > 0) {
         while($row = mysql_fetch_array($result)) {
             session_start();
             session_register('username');
             echo 'Success';
         }
     } else {
         echo 'Incorrect login name or password';
     }
 ?>

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\fjalor\admin\login.php:1) in C:\xampp\htdocs\fjalor\admin\login.php on line 2
Success


PS. It's just one row in the database
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Simple Login problem

Post by jackpf »

You still haven't put it at the top.

Also, if there's only 1 row, you don't need the while loop.
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Simple Login problem

Post by klevis miho »

Ok man im here now:

Code: Select all

 
 <?php
session_start();
$connection = mysql_connect('localhost','root','') or die(mysql_error());
$selectDB = mysql_select_db('fjalorinformatike') or die(mysql_error());
 
    $user = addslashes($_POST['username']);
     $pass = md5($_POST['password']);
   
     $result = mysql_query("SELECT * FROM perdorues WHERE username='$user' AND password='$pass'");
    
     $rowCheck = mysql_num_rows($result);
     
     if($rowCheck > 0) {
         $row = mysql_fetch_array($result);
 
             session_register('username');
             echo 'Success';
         
     } else {
         echo 'Incorrect login name or password';
     }
 ?>

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\fjalor\admin\login.php:1) in C:\xampp\htdocs\fjalor\admin\login.php on line 2

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\fjalor\admin\login.php:1) in C:\xampp\htdocs\fjalor\admin\login.php on line 2
Success


It must be a session thing. Im trying to learn sessions by that u know :)
ageofturb
Forum Newbie
Posts: 1
Joined: Tue Aug 25, 2009 9:04 pm

Re: Simple Login problem

Post by ageofturb »

Remember that you always need to put these things:
headers, session functions, cookies...

Code: Select all

 
header("xxx");
session_start(); session_destroy();
setCookie("xx");
 
before anything is output.
so put them before any HTML or any echo.

___________
Cheers blues all we want!
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Simple Login problem

Post by klevis miho »

I got the problem, it was that space before the <?php . What an errorr lol
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Simple Login problem

Post by jackpf »

Output includes white space ;)
Post Reply