Why a free hosting doesn't allow creating cookies? [duplicate] - php

This question already has answers here:
Warning: Cannot modify header information - headers already sent [duplicate]
(4 answers)
Closed 7 years ago.
This is my code:
<?php
$value = 'something from somewhere';
setcookie("TestCookie", $value);
?>
I am getting the error message as follows:
Warning: Cannot modify header information - headers already sent by
(output started at /home/nairsoft/public_html/page1.php:2) in
/home/nairsoft/public_html/page1.php on line 4
I am using a free webhosting server.

The headers are sent at the beginning of a request before the body of the server's response. It looks like your code already sent the headers so you can't modify them. As Afaan suggested, make sure you don't ouput anything before the opening php tag.
In general, it is a good idea to process everything you need at the beginning of the request before sending any of the response.

The above problem is solved by saving my code in ANSI instead of UTF-8

Cookie is a part of headers and headers must be sent before any actual output to the browser. Make sure that you set your cookies and other headers before you output anything. It has nothing whatsoever to do with what type of hosting package you have.

Related

Headers already sent cookies error [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 7 years ago.
I wanted to do "remember me" option in my login form but i still getting error:
;llfdfdffddffdfd
Warning: Cannot modify header information - headers already sent by (output started at /home/chumorekgn/www/maneku/log-in.php:40) in /home/chumorekgn/www/maneku/log-in.php on line 76
Warning: Cannot modify header information - headers already sent by (output started at /home/chumorekgn/www/maneku/log-in.php:40) in /home/chumorekgn/www/maneku/log-in.php on line 77
I dont know what should i do... Here is my code
if(isset($_GET['r'])){
echo ";llfdfdffddffdfd";
setcookie("Maneku_login", base64_encode($l));
setcookie("Maneku_pass", base64_encode(md5(md5(base64_decode($h)))));
}
when the request is made to the page the header is sent first and that header contains all the information like cookies, session and other useful information. When request is sent if the server finds the request similar to previous one then it will give response “header already sent”.
So the error in PHP is shown as following:
<?php
print “text”;
header(‘Location: http://www.example.com/’);
?>
Check reference for complete solution for issue: http://www.navnishbhardwaj.com/how-to-fix-headers-already-sent-error-in-php/

Having trouble with php code to redirect [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 8 years ago.
I am a beginner in php..
I need to redirect from one php file to another php file..
I used this code for this.
header("Location: stu_rep.html");
but it shows this error..
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\auttvl\admin\stu_rep_view.php:241) in C:\xampp\htdocs\auttvl\admin\stu_rep_view.php on line 492
I need to know what is wrong,Is there any other way to redirect without using header?
Thanks!
Header(); sends a HTTP header. But HTTP headers can only be sent before anything else. In your case, you are probably printing something out before using header();. Remove the printings and your done.
More informations here:
http://www.php.net/manual/de/function.header.php
Headers must be sent before any output is generated on the website. To "work around" this, you can add ob_start(); to the top of your file.

Cannot modify header information [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
my site works well in localhost (everything works fine)
but when I upload it in web and try to redirect the page after updating the data it says -> Cannot modify header information
95% you are trying to set a cookie, mime-type or redirection (short: http header) after you have sent http body, i.e. content. Check if you you have anything including whitespaces outside the php delimeters.
If you use the header function you have to be careful... You can't send any header before the "header location".
ob_start('mypage');
<html>
// Your code ...
</html>
ob_end_flush();
What's the error ? (php.ini, active the errors)
Else you can use the ob_start() function !

Header location issue, how to solve it? [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
I created a web site, with php and SQL using WampServer.
And after a condition or in boucle I use Header location to transfer the user to another page. But I got an error from the server, I think is because I use the header after a code and not in . I deleted all blank spaces.
if($passfinal['contrasena']==$_POST['password'])
{
$_SESSION['logedin']=TRUE;
$_SESSION['userid']=$passfinal['id'];
header('Location: ../index.php');
}
Do you have something to help me?
Thnak you.
I think, here is problem $_POST['password'], because before you make compare, first you must check if(isset($_POST['password'])), blank spaces isn't important in this case and last one: I advise you, that you must write full url in header function, like this: header('location: http://example.com/index.php'), because this is more nice and true way.
Warning: Cannot modify header information - headers already sent by (output started at /home/sirobdco/public_html/login/login.php:11) in /home/sirobdco/public_html/login/includes/loginform/loginform.php on line 37
You already sent headers so PHP cannot send them again!
That is, before your code
header('Location: ../index.php');
You already send headers - blank space in html, an echo in PHP, etc.
Have a read through this:
http://php.net/manual/en/function.header.php
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. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

COOKIES and "Cannot modify header information"? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Warning: Cannot modify header information. Can't find error
a tough question. :)
I have a php script that upon a form submit verifies username and password against a DB and, if they're OK,
setcookie("call_admin_uin", $login , $expire);
setcookie("call_admin_pass", $password , $expire);
There is NO html before that. Nothing is echo-ed out and it works fine on my localhost and on one other server. Yet, when I upload it to my main server it gives out this:
Warning: Cannot modify header information - headers already sent by (output started at /home7/pnstatsc/public_html/admin/index.php:6) in /home7/pnstatsc/public_html/admin/index.php on line 72
Warning: Cannot modify header information - headers already sent by (output started at /home7/pnstatsc/public_html/admin/index.php:6) in /home7/pnstatsc/public_html/admin/index.php on line 73
So I guess there could be something wrong with the server setting... any idea what it might be?
Thanks!
You probably have some whitespace before an opening <?php somewhere. First, I would see what the output of headers_list() is -- that will tell you which headers have been sent, then I would go through and see what headers_sent($filename) and, if necessary, headers_sent($filename, $line_number).
If you're desperate, then you can always use ob_start and buffer your output, but that is overkill.
There must have been some output.
You can use headers_sent to track down the file and line where headers have been sent.

Categories