Welcome! Log In Create A New Profile

Advanced

Moved code to server and many errors is shown

Posted by 47376120 - Bergie 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Moved code to server and many errors is shown
October 12, 2013 04:16PM
On my local linux server (Running on a Raspberry Pi) I was able to get my code working as I expect it to.

Now after moving my database and web pages I get an abundance off errors, although the code seem to work fine.


I get hese for every page I open that have a session_start() at the top.
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/##hashed_out_by_me##/login.php:4) in /home/##hashed_out_by_me##/login.php on line 4

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/##hashed_out_by_me##/login.php:4) in /home/##hashed_out_by_me##/login.php on line 4

Line 4 and the head looks like this.
Language: HTML
<!-- Start a the session --> <?php session_start(); ?>   <html> <head> <title>Club manager login</title> <script type="text/javascript">   <!-- This javascript function opens a new window with the plain text of this page --> function openTXT() { window.open(';login.txt';,';Page Code';,';width=300,height=500,toolbar=no,location=no,menubar=no,scrollbars=yes,resizable=yes';); } </script>     </head>

I can't understand what I am doing wrong since it works locally. I am considering submitting my local server where things seems to work fine.
avatar Re: Moved code to server and many errors is shown
October 12, 2013 04:43PM
Quote

I get hese for every page I open that have a session_start() at the top.

Your session_start() is NOT at the top - you output an HTML comment to the browser first.


First error -

Language: PHP
<!-- Start a the session --> // Remove everything before <?php, it sends output to the browser. <?php session_start(); ?>

Remove any code that sends output to the browser before the first <?php tag.
Or, use a PHP comment within the <?php tags, instead of an HTML comment.


Second error -
Note: Fixing the first error may also fix the second error, if not -

Edit the php.ini file -

session.cache_limiter = public // Make sure this is "public".
avatar Re: Moved code to server and many errors is shown
October 12, 2013 04:55PM
Quote

I can't understand what I am doing wrong since it works locally

The php.ini settings on your local computer are probably different from those on the server.

Run this script on the server to see the php.ini settings -

Language: PHP
<?php phpinfo(); ?>


Then delete it immediately - it is a goldmine for any hacker.
Re: Moved code to server and many errors is shown
October 14, 2013 09:16AM
Ok, I did not realise comments will be seen as part of the page. Moved it and the warnings disappeared.

Now I have used the "headers()" function to redirect and get this.
Warning: Cannot modify header information - headers already sent by (output started at /home/######/login.php:23) in /home/######/login.php on line 40

And here is the offender.
Language: PHP
header("Location: list_players.php");

And here is the current header.

Language: PHP
<?php /* This page should only be accessible from the manager';s session This part goes on top of every page only accessible to the club manager. It mean every page except for the public access and the login page itself. */ session_start(); /* Check if a session is open by verifying a session variable */ $working = $_SESSION[';started';]; if ($working != "yes") { header("Location: login.php"); } ?>   <html> <head> <title>Displaying player lists</title> <link href="style.css" rel="stylesheet" type="text/css" /> <script type="text/javascript"> <!-- This javascript function opens a new window with the plain text of this page --> function openTXT() { window.open(';list_players.txt';,';Page Code';,';width=300,height=500,toolbar=no,location=no,menubar=no,scrollbars=yes,resizable=yes';); } </script> </head>

This page headers() gives some good info on using the headers() function, but it looks like I am doing it all wrong. What would be the appropiate way to deal with headers? I am not clear on it.
Once again, my home server is happily dishing it all up as above, but not my online server.

I can get away with replacing line 40 with this, but it seems to be frowned upon - and requires some recoding.
Language: PHP
echo "<meta http-equiv=';refresh'; content=';0;url=list_players.php';>";
avatar Re: Moved code to server and many errors is shown
October 14, 2013 10:03AM
Home computer v Server -

Check the output_buffer settings in the php.ini files, they are quite likely different.


You should check that $_SESSION isset before trying to extract data from it.
It can output errors / warnings if there is a problem.

Make sure that there are no spaces or control codes before the opening <?php tag.

Some editors insert a control code at the beginning of the file - suggest you make sure that encoding is set to UTF-8 WITHOUT BOM.

Putting exit; immediately after the header(); line may also help.

Whitespace after the closing ?> php tag can sometimes also cause problems.
avatar Re: Moved code to server and many errors is shown
October 14, 2013 10:10AM
Quote

(output started at /home/######/login.php:23)

The problem seems to be on line 23.

I can't tell which line that is without line numbers.
Re: Moved code to server and many errors is shown
October 15, 2013 09:05AM
Line 23 is the quoted "header();" line.

If you call the header() function, are you allowed a "normal" html header (as in my quoted code) to still be at the top of the page? Header() that seem to call pages where there is no html coded header seems to be ok. I've worked this problem away with an html meta tag. I just left the original header() call there in comments with a note that the actual header() should be better to use. smile

On my home server I can easily get to the php.ini file, but have no idea how to get to the web server's php.ini file. In fact I don't believe one can change a shared hosting service's .ini file. Or maybe there is another learning curve for me. Right now, I have two weeks left to prepare for the python exam... So officially I will stop developing my Portfolio, but would still like to learn more. My two Raspberry Pi's don't have enough to do yet!
avatar Re: Moved code to server and many errors is shown
October 15, 2013 09:23AM
You cannot put ANYTHING before a header() call that outputs anything to the browser.

That includes whitespace, hidden control codes, and any HTML.


You can usually override default php.ini settings in an .htaccess file (on an Apache server), or else some of the .ini settings can be changed at runtime.
Have a look at the PHP documentation.


Some ISPs also allow you to insert a local php.ini file within your hosted directory structure, which allows you to override the default settings.
Re: Moved code to server and many errors is shown
October 29, 2013 01:46PM
I had the exact same issues this Topic addresses.
So these posts helped me alot and i'm all sorted now thanks everyone!
Sorry, only registered users may post in this forum.

Click here to login