Results 1 to 11 of 11

Thread: PHP Help - Why am I getting this error?

  1. #1
    Hexus.Lurker
    Join Date
    Jan 2010
    Posts
    357
    Thanks
    17
    Thanked
    13 times in 13 posts
    • Fortune117's system
      • Motherboard:
      • Gigabyte Z77-D3H
      • CPU:
      • Intel Core i5 3570k 3.4Ghz
      • Memory:
      • 8GB Corsair DDR3
      • Storage:
      • Kingston Hyper X 120GB SSD
      • Graphics card(s):
      • XFX Radeon 7870 DD Edition
      • PSU:
      • OCZ ZS Series 650 Watt
      • Case:
      • Coolermaster CM690
      • Operating System:
      • Windows 8 64-Bit
      • Monitor(s):
      • [Eyefinity] 3 LG 23EA63V
      • Internet:
      • Virgin Media 50MB

    PHP Help - Why am I getting this error?

    Okay, so im trying to do this login script for my college project. Why am I getting these errors:

    Code:
    Warning: session_register() [function.session-register]: Cannot send session cookie - headers already sent by (output started at /homepages/4/d399133263/htdocs/checklogin.php:8) in /homepages/4/d399133263/htdocs/checklogin.php on line 62
    
    Warning: session_register() [function.session-register]: Cannot send session cache limiter - headers already sent (output started at /homepages/4/d399133263/htdocs/checklogin.php:8) in /homepages/4/d399133263/htdocs/checklogin.php on line 62
    
    Warning: Cannot modify header information - headers already sent by (output started at /homepages/4/d399133263/htdocs/checklogin.php:8) in /homepages/4/d399133263/htdocs/checklogin.php on line 64
    
    Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively in Unknown on line 0
    The PHP behind this is as follows:

    Code:
    <?php
    ob_start();
    $host="MYDB; // Host name
    $username="DBUSER"; // Mysql username
    $password="Password"; // Mysql password
    $db_name="db404370762"; // Database name
    $tbl_name="members"; // Table name
    
    // Connect to server and select databse.
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
    
    // Define $myusername and $mypassword
    $myusername=$_POST['myusername'];
    $mypassword=$_POST['mypassword'];
    
    // To protect MySQL injection (more detail about MySQL injection)
    $myusername = stripslashes($myusername);
    $mypassword = stripslashes($mypassword);
    $myusername = mysql_real_escape_string($myusername);
    $mypassword = mysql_real_escape_string($mypassword);
    
    $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
    $result=mysql_query($sql);
    
    // Mysql_num_row is counting table row
    $count=mysql_num_rows($result);
    // If result matched $myusername and $mypassword, table row must be 1 row
    if($count==1){
    session_register("myusername");
    session_register("mypassword");
    header("location:login_success.php");
    }
    else {
    echo "Wrong Username or Password";
    }
    ob_end_flush();
    ?>

    The code that is in Bold is line 62, 63 and 64. Please help! What am I doing wrong here?

    -Fortune
    Last edited by Fortune117; 20-02-2012 at 07:54 PM. Reason: Changed DB info

  2. #2
    Senior Member
    Join Date
    Feb 2008
    Posts
    914
    Thanks
    4
    Thanked
    155 times in 143 posts
    • smargh's system
      • Motherboard:
      • Gigabyte GA-EP45-UD3P
      • CPU:
      • Xeon E5450 with 775-to-771 Mod
      • Memory:
      • 16GB Crucial
      • Storage:
      • Intel X25-M G2 80GB/Adaptec 3405 4x 2TB Ultrastar RAID1 / 1x 6TB Hitachi He6 / Dying 2TB Samsung
      • Graphics card(s):
      • GTX 750 Ti
      • PSU:
      • Seasonic X-560
      • Case:
      • Lian-Li PC-A71
      • Operating System:
      • Windows 7 Ultimate 64bit
      • Monitor(s):
      • BenQ G2400WD
      • Internet:
      • Really Crap ADSL2 <3Mbit

    Re: PHP Help - Why am I getting this error?

    Have you got any text, including any newlines, before this script block starts?

    Also, use parameterized queries to propery protect against SQL injection, unless anyone can correct me on that. PDO can do it.

  3. Received thanks from:

    Fortune117 (20-02-2012)

  4. #3
    HEXUS.social member Agent's Avatar
    Join Date
    Jul 2003
    Location
    Internet
    Posts
    19,168
    Thanks
    735
    Thanked
    1,607 times in 1,045 posts

    Re: PHP Help - Why am I getting this error?

    One of the first things that get sent to the browser are 'headers'. In their most simple form, they describe the data that is going to follow, how it's encoded, cache period and so on....
    It's the way your browser knows if a page is HTML and should be displayed, or a Word document and should be downloaded for example.

    As smargh says above, these headers have already been sent for some reason. This means that your scripts are sending information to the user in some form (echos, prints, raw HTML?) already, otherwise the headers wouldn't have been sent.

    As above - is this script one that's called from other scripts, or is it stand alone?
    Quote Originally Posted by Saracen View Post
    And by trying to force me to like small pants, they've alienated me.

  5. Received thanks from:

    Fortune117 (20-02-2012)

  6. #4
    Hexus.Lurker
    Join Date
    Jan 2010
    Posts
    357
    Thanks
    17
    Thanked
    13 times in 13 posts
    • Fortune117's system
      • Motherboard:
      • Gigabyte Z77-D3H
      • CPU:
      • Intel Core i5 3570k 3.4Ghz
      • Memory:
      • 8GB Corsair DDR3
      • Storage:
      • Kingston Hyper X 120GB SSD
      • Graphics card(s):
      • XFX Radeon 7870 DD Edition
      • PSU:
      • OCZ ZS Series 650 Watt
      • Case:
      • Coolermaster CM690
      • Operating System:
      • Windows 8 64-Bit
      • Monitor(s):
      • [Eyefinity] 3 LG 23EA63V
      • Internet:
      • Virgin Media 50MB

    Re: PHP Help - Why am I getting this error?

    Thanks for your replies guys. The full PHP document code is as follows:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>
    	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
    	<meta name="description" content="Connor Wilsons Blog" />
    	<meta name="keywords" content="" />
    	<meta name="author" content="Connor Wilson" />
    	<link rel="stylesheet" type="text/css" href="stylephp.css" media="screen,projection" />
    	<title>Avio Web Design - A Website By Connor Wilson</title>
    </head>
    <body>
    <div id="container" >
    	<div id="header">
    		<h1>Avio Web Design</h1>
    		<h2>Connor Wilson's Personal Blog and Online Portfolio</h2>
    	</div>
    	<div id="navigation">
    		<ul>
    			<li class="selected"><a href="index.htm">Home</a></li>
    			<li><a href="blog.htm">Blog</a></li>
    			<li><a href="tutorial.htm">Tutorials</a></li>
    			<li><a href="portfolio.htm">Portfolio</a></li>
    			<li><a href="wow.htm">WoW Portal</a></li>
    			<li><a href="btec.htm">BTEC ND Work</a></li>			
    			
    		</ul>
    	</div><div class="inner_copy"><div class="inner_copy"></div></div>
      <div id="content">
    		<center> <h2>Checking your details </h2> </center>
            
    <center>
    
    <?php
    ob_start();
    $host="dbhost"; // Host name
    $username="dbuser"; // Mysql username
    $password="dbpass"; // Mysql password
    $db_name="dbname"; // Database name
    $tbl_name="members"; // Table name
    
    // Connect to server and select databse.
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
    
    // Define $myusername and $mypassword
    $myusername=$_POST['myusername'];
    $mypassword=$_POST['mypassword'];
    
    // To protect MySQL injection (more detail about MySQL injection)
    $myusername = stripslashes($myusername);
    $mypassword = stripslashes($mypassword);
    $myusername = mysql_real_escape_string($myusername);
    $mypassword = mysql_real_escape_string($mypassword);
    
    $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
    $result=mysql_query($sql);
    
    // Mysql_num_row is counting table row
    $count=mysql_num_rows($result);
    // If result matched $myusername and $mypassword, table row must be 1 row
    if($count==1){
    session_register("myusername");
    session_register("mypassword");
    header("location:login_success.php");
    }
    else {
    echo "Wrong Username or Password";
    }
    ob_end_flush();
    
    ?>
    <p>&nbsp;</p>
    	</div>
    	
    	<div id="footer">
    		<div class="fcenter">
    			
    			<div class="fcenter"><p><a href="/login.php"> Avio Web Designs 2012</a> </p></div>
    		</div>
    	</div>
    
    </div>
    </body>
    </html>
    There are two other scripts, main_login.php is a basic web page with username and password entry fields and loginsuccess.php is where the user gets directed when the details are verified

  7. #5
    HEXUS.social member Agent's Avatar
    Join Date
    Jul 2003
    Location
    Internet
    Posts
    19,168
    Thanks
    735
    Thanked
    1,607 times in 1,045 posts

    Re: PHP Help - Why am I getting this error?

    You have HTML / browser output before you're asking to define the header manually. Simply put the PHP before it / design it like that from the start
    Quote Originally Posted by Saracen View Post
    And by trying to force me to like small pants, they've alienated me.

  8. #6
    Hexus.Lurker
    Join Date
    Jan 2010
    Posts
    357
    Thanks
    17
    Thanked
    13 times in 13 posts
    • Fortune117's system
      • Motherboard:
      • Gigabyte Z77-D3H
      • CPU:
      • Intel Core i5 3570k 3.4Ghz
      • Memory:
      • 8GB Corsair DDR3
      • Storage:
      • Kingston Hyper X 120GB SSD
      • Graphics card(s):
      • XFX Radeon 7870 DD Edition
      • PSU:
      • OCZ ZS Series 650 Watt
      • Case:
      • Coolermaster CM690
      • Operating System:
      • Windows 8 64-Bit
      • Monitor(s):
      • [Eyefinity] 3 LG 23EA63V
      • Internet:
      • Virgin Media 50MB

    Re: PHP Help - Why am I getting this error?

    So like this? :

    Code:
    <?php
    ob_start();
    $host="DB"; // Host name
    $username="DB"; // Mysql username
    $password="DB"; // Mysql password
    $db_name="DB"; // Database name
    $tbl_name="members"; // Table name
    
    // Connect to server and select databse.
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
    
    // Define $myusername and $mypassword
    $myusername=$_POST['myusername'];
    $mypassword=$_POST['mypassword'];
    
    // To protect MySQL injection (more detail about MySQL injection)
    $myusername = stripslashes($myusername);
    $mypassword = stripslashes($mypassword);
    $myusername = mysql_real_escape_string($myusername);
    $mypassword = mysql_real_escape_string($mypassword);
    
    $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
    $result=mysql_query($sql);
    
    // Mysql_num_row is counting table row
    $count=mysql_num_rows($result);
    // If result matched $myusername and $mypassword, table row must be 1 row
    if($count==1){
    session_register("myusername");
    session_register("mypassword");
    header("location:login_success.php");
    }
    else {
    echo "Wrong Username or Password";
    }
    ob_end_flush();
    
    ?>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>
    	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
    	<meta name="description" content="Connor Wilsons Blog" />
    	<meta name="keywords" content="" />
    	<meta name="author" content="Connor Wilson" />
    	<link rel="stylesheet" type="text/css" href="stylephp.css" media="screen,projection" />
    	<title>Avio Web Design - A Website By Connor Wilson</title>
    </head>
    
    <body>
    <div id="container" >
    	<div id="header">
    		<h1>Avio Web Design</h1>
    		<h2>Connor Wilson's Personal Blog and Online Portfolio</h2>
    	</div>
    	<div id="navigation">
    		<ul>
    			<li class="selected"><a href="index.htm">Home</a></li>
    			<li><a href="blog.htm">Blog</a></li>
    			<li><a href="tutorial.htm">Tutorials</a></li>
    			<li><a href="portfolio.htm">Portfolio</a></li>
    			<li><a href="wow.htm">WoW Portal</a></li>
    			<li><a href="btec.htm">BTEC ND Work</a></li>			
    			
    		</ul>
    	</div><div class="inner_copy"><div class="inner_copy"></div></div>
      <div id="content">
    		<center> <h2>Checking your details </h2> </center>
            
    <center>
    
    
    <p>&nbsp;</p>
    	</div>
    	
    	<div id="footer">
    		<div class="fcenter">
    			
    			<div class="fcenter"><p><a href="/login.php"> Avio Web Designs 2012</a> </p></div>
    		</div>
    	</div>
    
    </div>
    </body>
    </html>
    Still doesnt work :/ Is there something im missing here? lol

  9. #7
    Hexus.Lurker
    Join Date
    Jan 2010
    Posts
    357
    Thanks
    17
    Thanked
    13 times in 13 posts
    • Fortune117's system
      • Motherboard:
      • Gigabyte Z77-D3H
      • CPU:
      • Intel Core i5 3570k 3.4Ghz
      • Memory:
      • 8GB Corsair DDR3
      • Storage:
      • Kingston Hyper X 120GB SSD
      • Graphics card(s):
      • XFX Radeon 7870 DD Edition
      • PSU:
      • OCZ ZS Series 650 Watt
      • Case:
      • Coolermaster CM690
      • Operating System:
      • Windows 8 64-Bit
      • Monitor(s):
      • [Eyefinity] 3 LG 23EA63V
      • Internet:
      • Virgin Media 50MB

    Re: PHP Help - Why am I getting this error?

    Update: Ive also tried JUST the code in a PHP document and still no luck :/

  10. #8
    Funking Prink! Raz316's Avatar
    Join Date
    Jul 2003
    Location
    Deal, Kent, UK
    Posts
    2,978
    Thanks
    130
    Thanked
    62 times in 52 posts

    Re: PHP Help - Why am I getting this error?

    The use of the function session_register is causing a warning to be shown, which in turn stops the redirect from happening, because output has already been displayed.

    I would recommend adding

    session_start();

    to the very top of the php script (just after the <?php )

    and changing
    Code:
    session_register("myusername");
    session_register("mypassword");
    to
    Code:
    $_SESSION['myusername'] = $myusername;
    $_SESSION['mypassword'] = $mypassword;
    Even then though, I see no reason why you would need to store the password in the session and would recommend not doing this.
    Last edited by Raz316; 21-02-2012 at 11:56 AM.

  11. Received thanks from:

    Fortune117 (21-02-2012)

  12. #9
    Hexus.Lurker
    Join Date
    Jan 2010
    Posts
    357
    Thanks
    17
    Thanked
    13 times in 13 posts
    • Fortune117's system
      • Motherboard:
      • Gigabyte Z77-D3H
      • CPU:
      • Intel Core i5 3570k 3.4Ghz
      • Memory:
      • 8GB Corsair DDR3
      • Storage:
      • Kingston Hyper X 120GB SSD
      • Graphics card(s):
      • XFX Radeon 7870 DD Edition
      • PSU:
      • OCZ ZS Series 650 Watt
      • Case:
      • Coolermaster CM690
      • Operating System:
      • Windows 8 64-Bit
      • Monitor(s):
      • [Eyefinity] 3 LG 23EA63V
      • Internet:
      • Virgin Media 50MB

    Re: PHP Help - Why am I getting this error?

    Ok, finally getting somewhere here! haha, thank you all for your replies.

    So now I get re-directed but this error pops up

    Code:
    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /homepages/4/d399133263/htdocs/login_success.php:8) in /homepages/4/d399133263/htdocs/login_success.php on line 31
    And on line 31 I have

    Code:
    	
    	<?
    session_start();
    if(!session_is_registered(myusername)){
    header("location:main_login.php");
    }
    ?>
    Again, thanks for the help.

  13. #10
    Funking Prink! Raz316's Avatar
    Join Date
    Jul 2003
    Location
    Deal, Kent, UK
    Posts
    2,978
    Thanks
    130
    Thanked
    62 times in 52 posts

    Re: PHP Help - Why am I getting this error?

    You need to have session_start right at the very top, make sure there isn't any white space at the start.

    For example, this is wrong

    Code:
    
    
    <?php
        session_start();
    
    
    // other stuff here
    this is right

    Code:
    <?php
        session_start();
    
    
    // other stuff here

  14. Received thanks from:

    Fortune117 (22-02-2012)

  15. #11
    Hexus.Lurker
    Join Date
    Jan 2010
    Posts
    357
    Thanks
    17
    Thanked
    13 times in 13 posts
    • Fortune117's system
      • Motherboard:
      • Gigabyte Z77-D3H
      • CPU:
      • Intel Core i5 3570k 3.4Ghz
      • Memory:
      • 8GB Corsair DDR3
      • Storage:
      • Kingston Hyper X 120GB SSD
      • Graphics card(s):
      • XFX Radeon 7870 DD Edition
      • PSU:
      • OCZ ZS Series 650 Watt
      • Case:
      • Coolermaster CM690
      • Operating System:
      • Windows 8 64-Bit
      • Monitor(s):
      • [Eyefinity] 3 LG 23EA63V
      • Internet:
      • Virgin Media 50MB

    Re: PHP Help - Why am I getting this error?

    Thanks to everyone that has replied, the problem is now fixed!

    Silly little errors like white space in my code.. Should really sort that out!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •