To avoid illegal use I check the login status at the beginning of the code. I do this as follows:
if (!isset($_SESSION['loggedin'])){
header('Location:http://www.name.nl/prg/login.php');
exit();
}
This works. But if I use this code first it doesn't work.
ini_set('session.save_path',realpath(dirname($_SERVER['DOCUMENT_ROOT'])).'/name.nl/tmp');
session_start();
if (!isset($_SESSION['loggedin'])){
header('Location:http://www.name.nl/prg/login.php');
exit();
}
Am I missing something? I looked for 2 days now, but can't find a reason/solution. It seems to me that the header function should work after ini_set and session_start. I mean it is common code?
Try this first: ini_set('session.save_path',realpath(dirname($_SERVER['DOCUMENT_ROOT']).'/name.nl/tmp'));
(Move parentheses)
Try looking here: http://php.net/manual/en/function.session-save-path.php
Not sure if this will help or not but it seems related to where your session folder is at and then pointing to the proper directory/folder in your code.
The solution that worked for them:
ini_set('session.save_path',realpath(dirname($_SERVER['DOCUMENT_ROOT']) . '/../session'));
Your current session save:
ini_set('session.save_path',realpath(dirname($_SERVER['DOCUMENT_ROOT'])).'/name.nl/tmp');
Well I found the cause. I had an "echo statement" before the header statement. After deleting this statement it works fine.
Thx for your comment anyway!
Related
It is defined in a config.php file
define("url_money", "www.himanshu1234.net63.net/MoneyManager/");
it is included in some other file like
include_once './config.php';
and used like this
<?php
header('Location: '.url_money.'login.php');
}
?>
there is some problem in it but unable to sort out.. !!
Change
define("url_money", "www.himanshu1234.net63.net/MoneyManager/");
to
define("url_money", "http://www.himanshu1234.net63.net/MoneyManager/");
When you define some link without http:// then it will be treated as relative link. So you were actually redirected to /www.himanshu1234.net63.net/MoneyManager/
I had an issue like this, and I made a stupid mistake. Then I saw
It is important to notice that header() must be called before any actual output is sent
So, please double-check whether there is any echo or printf, etc... costed me a few hours some years ago!
My code looks like this:
...
$_SESSION['message']="something";
header('location:http://url/somewhere');
exit;
As you can see, I have an exit at the end of it. And that is the problem. It doesn´t work although I have an exit there.
I have this problem only on my localhost. At the online server it works well. In the errorlog it only shows "undefined index message". A few days ago I installed new Apache 2.4 and PHP 5.4.
Don't forget to start your session on every page you are going to use it:
if(!isset($_SESSION)){
session_start();
}
Please be sure to add "session_start();" to the beginning of every page that processes any sort of session data. Another thing to check would be to make sure you are setting the session variable properly. Also, when using header to redirect, make sure there is NO white space in the beginning of your document.
I used to use this function to redirect from one php page to another:
header( 'Location: student.php?cnp='.$_REQUEST['name']) ;
In my localhost it does work, but if testing it in internet, it doesn't redirect.
I've also tried to give the full path (like http://.../student.php?...) but still it doesn't work. Does anyone know why and what should I do?
Try this:
header('Location: http://www.example.com/someurl.php', true);
Second parameter replaces previous location header (if any).
Also check what HTTP response code you receive (it should be 30X)
If nothing helps you can always redirect by javascript:
echo "<script>window.top.location='http://www.example.com/someurl.php'</script>"
This is not so professional but works even if headers has already been sent.
Try adding session_write_close(); before and exit() after:
session_write_close();
header( 'Location: student.php?cnp='.$_REQUEST['name']) ;
exit();
Just saw the error message in the comments. Add ob_start(); to the top of your pages, under the session_start() if you have that.
I tried this solution and it didnt work
header('Location: http://www.example.com/someurl.php', true);
So I suggest people to go with these two possible below solutions which worked for me.
using JS:
echo "<script>window.top.location='http://www.example.com/someurl.php'</script>"
using META:
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=http://www.example.com/someurl.php">';
These two both worked for me. But for some reason header thing doesnt work on the net but works locally perfectly fine. I am gonna be using meta solution.
Your header() is not working because the header was already sent to the browser.
Use ob_start() at the beginning of your page (even before the DOCTYPE declaration). And at the end, use ob_end_flush()
Try This www.psychocodes.in
<?php
ob_start();
//more code
header("Location:URL");
ob_end_flush();
?>
I just had the same problem this advice worked for me. Place this function at the top of your code:
function move_to(){
header('Location: student.php?cnp='.$_REQUEST['name']);
}
and call that function anywhere with:
move_to()
Btw, I'm not gonna take credit for it. I just found it at: Header function not working PHP
I wrote
<?
header("Location:http://example.com");
?>
but Redirect is not occured.
How to redirect?
But I do not have authority to edit php.ini
So safe_mode is on in php.ini
Try:
header("Location: http://example.com");
HTTP headers need to exactly follow the spec. More directly here (Location header):
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30
One possible issue is that there was something got "printed out" before you issue the above code. So check your code so that there is nothing got "echoed" before reached this line.
Two things:
You have to make sure you haven't sent any other HTML before sending your header.
You should also exit or die() after your header() call.
See this post for more detailed information.
You can also use JavaScript to do the redirect but I suspect PHP is probably a better idea in your situation.
Make sure you alway add die() after the header() call. This is extremely important if anything is output below the header() that the user is not supposed to see.
Make sure you have nothing prior to the opening "
If that still doesn't work, are you getting any sort of error message?
Alternatively, use:
<meta http-equiv="refresh" content="0;url=http://foo.com">
somewhere in your <head> section.
Source.
I know this has been covered before but I cannot find an answer to this,
I have always used this;
header("Location: http://www.website.com/");
exit();
This has always worked in my current project and all of a sudden it is not working in any of my browsers
I would like to figure out the problem and fix it instead of using
echo "<script type='text/javascript'>window.top.location='http://website.com/';</script>";
I also have error reporting enabled and it shows no errors
// SET ERROR REPORTING
error_reporting(E_ALL ^ E_WARNING ^ E_NOTICE);
ini_set('display_errors', TRUE);
Any ideas why it will not work?
Try:
error_reporting(E_ALL | E_WARNING | E_NOTICE);
ini_set('display_errors', TRUE);
flush();
header("Location: http://www.website.com/");
die('should have redirected by now');
See what you get. You shouldn't use ^ (xor) in your error_reporting() call because you're unintentionally asking for all errors EXCEPT notices and warnings.. which is what a 'headers already sent' error is.
Edit:
Also try putting flush() right above your header() call.
COMMON PROBLEMS:
1) there should not be any output (i.e. echo.. or HTML codes) before the header(.......); command.
2) there should not be a white-space(or newline) before <?php and after ?> tags.
3) GOLDER RULE! - the file (and other include()-d files) should have UTF8 without BOM encoding (and not just UTF-8). That is problem in many cases (because typical UTF8 encoded file has something special character in the start of file output)!!!!!!!!!!!
4) When redirecting, after header(...); you must use exit;
5) Recommended practice - always use 301 or 302 in reference:
header("location: http://example.com", true, 301 ); exit;
6) If none of above helps, use JAVSCRIPT redirection(but it's highly not recommended By Google), but it may be the last chance...:
echo "<script type='text/javascript'>window.top.location='http://example.com/';</script>"; exit;
Try removing the Space Between location and the first h in http.
header("Location: http://www.website.com/");
exit();
turns into
header("Location:http://www.website.com/");
exit();
I had this problem on my WAMP Server.
Although it shouldn't be the problem, considering that is how it is documented in the PHP documentation. But you should probably try it anyway. I know it has worked for me in a number of cases.
try this. worked for me.
echo "<meta http-equiv='refresh' content='0;url=http://www.yoursite.com'>";
Also when you are using the header function it has to be the first thing called before any text (even a space) is written to the client, so check again that there is no spaces being output prior to your call even before th
<?php
It may be strange solution but try this, change the page encoding from utf8 to ANSI and it will work.
use any text editor and save the page as ANSI encoding and upload it to your online server.
Adding ob_start() solved this issue.
Try this (working for me):
echo '<script>window.location.replace("http://www.example.com")</script>';
Instead of
header("Location: http://www.example.com");
What exactly happens when you visit the page? You can try Firebug or any other tool that allows you to analyze HTTP headers and check if the redirect really happens and whether the Location header is really present.
You should also verify that you are redirecting to a valid location, and that the location has proper 404 and 500 error messages/pages setup. It could be that you are simply redirecting a bad place.
Weird, but removing blank lines in php worked for me :-\
code before:
<?php
header("Location: http://www.website.com/");
?>
code that worked:
<?php header("Location: http://www.website.com/"); ?>
If your index page is a html file, it may not work. Change it to index.php and use this code:
<?php
header("Location: http://ea.tc");
?>
I actually had a case similar to this where I had an admin page that was included at the top of all my other pages. At the top of each page below the line:
<?php include '../../admin.php' ?>
I would have the php logic:
<?php if($_SESSION['username'] === null){ header("Location: ./adminLogin.php");}?>
The problem with this was that somewhere else I was also calling/manipulating the header(.... After a lot of time going through my code I admit I could not figure out where the problem was. Then I thought that each of these files hits my admin.php file before doing anything else. So I thought about what would happen if I would put the logic that was at the top of each of my views (because I didn't want anything to be visible unless you were logged in) into my admin.php file?
What happened was that before it even got to any of the php/html in my views it evaluated whether or not someone was logged in ($_SESSION['username'])) and if it was NULL then I just redirected to the adminLogin page. I put this logic right before my switch and it's worked perfectly for all my files that once required the logic. The way I had it worked in development, but posed a lot of issues in production. I found that moving the redirection logic to my admin.php file not only avoided the duplicate header(... manipulation but actually made my code more efficient by removing the excess logic from my view files and into my admin.php file.
Rather than putting the logic in every view file, put it in your controller once, before your switch. Works like a charm! This is useful if you don't want anyone to access any of the sensitive views unless they log in. In my case this was important for my CMS. However, if there are some files that you want to be viewable without logging in then I think the original logic would be sufficient. It seems like you already found a solution but hopefully this can be helpful if you run into this error again. :)