Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-21-2012, 01:03 AM   PM User | #1
BrianMartin&Co
New Coder

 
Join Date: Mar 2012
Location: United Kingdom
Posts: 30
Thanks: 7
Thanked 0 Times in 0 Posts
BrianMartin&Co is an unknown quantity at this point
PHP Session_Start()

Hello All,

Firstly, thank you for taking the time to resolve my issue. The script was built to build up lead generation for my company. However I have come across a problem I haven't come across before.

if you go to bmandcoweb.co.uk/email.php the script works perfectly.

However when I try and intergrate this with other pages within my domain name. It doesn't work.

The Below Code
.co.uk/email.php

The blow code is simple. Once the user submits it's company name (username) and e-mail address an e-mail is sent with a download link. I still need to clean up how the e-mail is sent but this is irrelevant at the moment.

PHP Code:
<?php
require('includes/init.php');

?>
<div align="center">
  <h2 >Email Form</h2></div><br />
<div align="center" style="width: 600px; margin: 0 auto;">
<?php
if(isset($_POST['username'], $_POST['email'])){
    
$username mysql_real_escape_string($_POST['username']);
    
$email    mysql_real_escape_string($_POST['email']);
    
$errors   = array();
    
$get_dom  end(explode('@'$email));
    if(empty(
$username)||empty($email)){
        
$errors[] = 'You must fill in both fields.';
    } else {
        if(
mysql_result(mysql_query("SELECT COUNT(`id`) FROM `companies` WHERE `username`='$username'"), 0) == 1){
            
$errors[] = 'That username already exists.';
        }
        if(
filter_var($emailFILTER_VALIDATE_EMAIL) === false){
            
$errors[] = 'E-mail address is not valid.';
        }

    }
    if(!empty(
$errors)){
        foreach(
$errors as $error){
            echo 
'<font size="+1" color="#FF0000">'.$error.'</font><br />';
        }
    } else {
        
        
mysql_query("INSERT INTO `companies` (`id`,`username`,`email`) VALUES ('','$username','$email');");
        
$body 'Welcome, '.$email.'!

This e-mail is to verify that your deatails are correct and to download our executive summary document.

<a href="pdf/executive-summary.pdf.pdf">Download Ececutive Sumary</a>
<br />
Your Company Name is: '
.$username.
'
;
        
mail($email'You have submitted your details'$body'From: brian@bmandco.co.uk');
        print(
'Details Submitted!');
    }
}

?>
<form action="" method="post">
<p>Company Name:<br /><input type="text" name="username" maxlength="35" /></p>
<p>E-Mail Address::<br /><input type="text" name="email" maxlength="255" /></p>
<p><input type="submit" value="Submit Details" /></p>
</form>
</div>
The Blow Code
includes/init.php
The database and passwords are included as normal.
PHP Code:
<?php
ob_start
();
session_start();
mysql_connect('localhost''database','password');
mysql_select_db('database');
require(
'functions/codes.func.php');
require(
'functions/wsite.func.php');

require(
'functions/users.func.php');
if(
$_SERVER['PHP_SELF'] == '/account/edit.php'){
    
$profile_emotes_show '<script src="/javascript/emotes.js" type="text/javascript"></script>
'
;} elseif($_SERVER['PHP_SELF'] == '/account/upload-vid.php' || $_SERVER['PHP_SELF'] == '/account/upload-music.php' || $_SERVER['PHP_SELF'] == '/account/upload-img.php' || $_SERVER['PHP_SELF'] == '/account/upload-work.php') {
    
$show_upload_emotes '<script src="/javascript/upload_emotes.js" type="text/javascript"></script>
'
;} elseif($_SERVER['PHP_SELF'] == '/account/profile.php') {
    
$profile_show '<script src="/javascript/sho_emo.js" type="text/javascript"></script>
'
;} elseif($_SERVER['PHP_SELF'] == '/admin/edit-upload.php') {
    
$edit_upload '<script src="/javascript/upload_emotes.js" type="text/javascript"></script>
'
;}
?>
if you go to the bmandcoweb.co.uk website you'll find that it's claiming it can't work because of

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/bmandco/public_html/index.php:36) in /home/bmandco/public_html/includes/init.php on line 3

however it works perfectly in .co.uk/email.php

Please shed some light on this - Please

Brian

Last edited by ShaneC; 04-21-2012 at 04:15 AM.. Reason: I forgot my manners
BrianMartin&Co is offline   Reply With Quote
Old 04-21-2012, 01:14 AM   PM User | #2
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,521
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Brian,

For some reason the error message says the error has occurred in index.php yet you've chosen to show us email.php which you openly admit works without a problem.

How do you expect us to advise you on what is wrong and how to fix index.php if you're not going to show us the code? Why are you showing us the code for email.php which you say works and has no problems?

All is not lost however. If you don't want to show us the code for index.php (which you've made quite clear by not posting it despite knowing its at fault) then I have a handy TIP in my signature which explains this error and how to deal with it. I would recommend you read it as it may help you to resolve your problem yourself.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 04-21-2012, 01:14 AM   PM User | #3
ShaneC
Codeasaurus Rex


 
Join Date: Jun 2008
Location: Redmond, WA
Posts: 659
Thanks: 31
Thanked 100 Times in 94 Posts
ShaneC is on a distinguished road
You'll want to go ahead and switch the following lines in init.php.

Change:

Code:
ob_start();
session_start();
to

Code:
session_start();
ob_start();
For more detail on this area, see this article.

And thank you for the politeness! Always appreciated.
__________________
Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
My posts, comments, code, and suggestions reflect only my personal views.
Web Portfolio and Code Snippets: http://shanechism.com
ShaneC is offline   Reply With Quote
Users who have thanked ShaneC for this post:
BrianMartin&Co (04-21-2012)
Old 04-21-2012, 01:18 AM   PM User | #4
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,521
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
@ShaneC: No, if init.php works fine with email.php and NOT for index.php then the order of those two lines will make no difference (I had initially thought the same thing). The problem here is that (as the message says) the headers have already been sent because output has already been sent to the browser in index.php somewhere.

Even in the article it quite clearly shows that you can do this:
Quote:
PHP Code:
//Start buffering the output
ob_start();

print(
"Some text");

//Since the output is now buffered, the above statement will not cause any error
session_start(); 
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 04-21-2012, 01:20 AM   PM User | #5
ShaneC
Codeasaurus Rex


 
Join Date: Jun 2008
Location: Redmond, WA
Posts: 659
Thanks: 31
Thanked 100 Times in 94 Posts
ShaneC is on a distinguished road
Quote:
Originally Posted by tangoforce View Post
@ShaneC: No, if init.php works fine with email.php and NOT for index.php then the order of those two lines will make no difference (I had initially thought the same thing). The problem here is that (as the message says) the headers have already been sent because output has already been sent to the browser in index.php somewhere.
And yet if output buffering is enabled content won't be sent to the browser until flush or termination.

I will concede that, after looking at the error again, the problem may well be in the index.php file (I erroneously assumed it was in init). I would still recommend the original poster make the modification I suggested, however, to alleviate future confusion.
__________________
Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
My posts, comments, code, and suggestions reflect only my personal views.
Web Portfolio and Code Snippets: http://shanechism.com
ShaneC is offline   Reply With Quote
Users who have thanked ShaneC for this post:
BrianMartin&Co (04-21-2012)
Old 04-21-2012, 01:23 AM   PM User | #6
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,521
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
I've already looked at the article and it clearly shows the opposite of what you've actually said about those two lines. Note that output buffering is started inside init.php - not inside index.php as far as we know.

The point is that Brian has clearly stated that the init.php file works fine with another file but not for the file that claims its already sent its headers. If init.php works fine elsewhere then it's nothing to do with the order of those two lines.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 04-21-2012, 01:26 AM   PM User | #7
BrianMartin&Co
New Coder

 
Join Date: Mar 2012
Location: United Kingdom
Posts: 30
Thanks: 7
Thanked 0 Times in 0 Posts
BrianMartin&Co is an unknown quantity at this point
Hey thanks for those tips. The swaping line session and line ob_start(); didn't work

However; I didn't want to post the entire index.php as I'm using functions, the problem occurs on every page so what makes index.php different to say seo-services.php ect


Brian
BrianMartin&Co is offline   Reply With Quote
Old 04-21-2012, 01:28 AM   PM User | #8
ShaneC
Codeasaurus Rex


 
Join Date: Jun 2008
Location: Redmond, WA
Posts: 659
Thanks: 31
Thanked 100 Times in 94 Posts
ShaneC is on a distinguished road
Quote:
Originally Posted by BrianMartin&Co View Post
Hey thanks for those tips. The swaping line session and line ob_start(); didn't work

However; I didn't want to post the entire index.php as I'm using functions, the problem occurs on every page so what makes index.php different to say seo-services.php ect


Brian
And yet we really need to see that page in order to help you, especially if your functions are what's causing the problem.

If you don't want it publicly posted, feel free to PM it to me. Otherwise, please post at the very least lines 30 to 40 of index.php.
__________________
Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
My posts, comments, code, and suggestions reflect only my personal views.
Web Portfolio and Code Snippets: http://shanechism.com
ShaneC is offline   Reply With Quote
Users who have thanked ShaneC for this post:
BrianMartin&Co (04-21-2012)
Old 04-21-2012, 01:32 AM   PM User | #9
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,521
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by BrianMartin&Co View Post
Hey thanks for those tips. The swaping line session and line ob_start(); didn't work
No it won't because as I've said, you have output starting in your other pages before your inclusion of init.php. It works in email.php because it's included straight away.

Quote:
Originally Posted by BrianMartin&Co View Post
However; I didn't want to post the entire index.php as I'm using functions, the problem occurs on every page so what makes index.php different to say seo-services.php ect
Functions or no functions, you're going to have to show something codewise. I've already said that line swapping wouldn't resolve this so you have two options:
1) Post code
2) Read the tip in my signature, modify your pages to use output buffering at the top of the code rather than inside init.php.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 04-21-2012, 01:35 AM   PM User | #10
BrianMartin&Co
New Coder

 
Join Date: Mar 2012
Location: United Kingdom
Posts: 30
Thanks: 7
Thanked 0 Times in 0 Posts
BrianMartin&Co is an unknown quantity at this point
Hey,

I have PM'd you both.

Again, thank you for your time and patients.

Brian
BrianMartin&Co is offline   Reply With Quote
Old 04-21-2012, 01:38 AM   PM User | #11
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,521
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by BrianMartin&Co View Post
I have PM'd you both.
Nope, you've not PM'd me so despite being able to see the fault from the first reply I made, you've chosen to ignore my help and advice and instead go with ShaneC's who was wrong (I presume because he's a Mod you thought he'd be a better bet?)

Well, not much I can say to that is there other than good luck - I will assist no more.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 04-21-2012, 01:41 AM   PM User | #12
BrianMartin&Co
New Coder

 
Join Date: Mar 2012
Location: United Kingdom
Posts: 30
Thanks: 7
Thanked 0 Times in 0 Posts
BrianMartin&Co is an unknown quantity at this point
I did try that and it didn't work. Perhaps I've done something wrong.
BrianMartin&Co is offline   Reply With Quote
Old 04-21-2012, 01:45 AM   PM User | #13
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,521
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Two minutes after my post you decide to PM me...

The cause is immediately obvious:

The first line in your index.php page is html code - OUTPUT THAT TRIGGERS YOUR HEADERS. Didn't you read the tip in my signature?

The very first thing you should have at the top of the file is this:
<?php require('includes/init.php'); ?>

It should look like this:
PHP Code:
<?php require('includes/init.php'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.

Last edited by ShaneC; 04-21-2012 at 02:57 AM.. Reason: OP's code removed by request
tangoforce is offline   Reply With Quote
Old 04-21-2012, 01:51 AM   PM User | #14
BrianMartin&Co
New Coder

 
Join Date: Mar 2012
Location: United Kingdom
Posts: 30
Thanks: 7
Thanked 0 Times in 0 Posts
BrianMartin&Co is an unknown quantity at this point
That was one of the things I done first in the early stages of attempting to solve my issue. and yes although it takes the error messages away from the website. the script now doesn't work.

It wont send the e-mail.
BrianMartin&Co is offline   Reply With Quote
Old 04-21-2012, 01:51 AM   PM User | #15
ShaneC
Codeasaurus Rex


 
Join Date: Jun 2008
Location: Redmond, WA
Posts: 659
Thanks: 31
Thanked 100 Times in 94 Posts
ShaneC is on a distinguished road
Well now that the file has been posted, and tangoforce and I have both told the original poster the same advice, it's probably best to converge back here.

The original poster specified via PM that adding <?php require('includes/init.php'); ?> to the top of the file does not fix the error. OP: Please double check that when you add that line to the top of index.php that you also remove it from wherever it is currently called.

If that still doesn't fix your problem, it may be helpful to see the contents of <?php include("functions/container.php");?>, which is referenced in the PHP error.
__________________
Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
My posts, comments, code, and suggestions reflect only my personal views.
Web Portfolio and Code Snippets: http://shanechism.com
ShaneC is offline   Reply With Quote
Users who have thanked ShaneC for this post:
BrianMartin&Co (04-21-2012)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:34 PM.


Advertisement
Log in to turn off these ads.