I have a simple PHP script that is supposed to redirect to another document after running some code, like so:
if...{
$_SESSION['username'] = $_POST['username'];
$_SESSION['login_status'] = true;
header('location: index.php');
}
else{...
The script works fine on one of the servers I have tried it on but not on the other. As it seems the other server doesn't go to the 'header' row but just stops running half way. All I am left with is a blank page with the title of the previous page. Why is that? Any ideas?
The PHP version on the server that runs the script is 5.1.6, the server that doesn't 4.3.9, not that I believe that has anything to do with it.
Did you started the session? Or your server might be just misconfigured.
Read the logs.
Check from php settings whether session.auto_start is enabled.
Also, it might be that older version of PHP doesn't know how to parse the location: ... string.
So change it to uppercase (so it conforms to HTTP specification):
header('Location: index.php')
Run your code with error reporting, so that you will get some clue about the error
error_reporting(E_ALL);
See PHP Error reporting
Related
I encountered the following problem:
I have a very simple script which forwards the user to this login script via POST:
session_start();
$_SESSION["job"] = "admin";
$_SESSION["login"] = true;
$_SESSION["email"] = "email#example.com";
$_SESSION["username"] = "example";
session_write_close(); # I added this because it was recommendet to solve my problem, which it unforunately doesn't do.
header("Location: ../home");
As soon as this script is executed everything is blocked which executes session_start(); in its script. The TTFB is always above 60s
The only fix is to restart apache via service apache2 restart every time.
My Apache2and PHP are clean installed today on an absolute fresh server, everything was configured according to the official instructions. Additionally I encounter this problem on every other server I ran my script on.
What could be a solution? I've searched the internet for days now...
As read from the above comments it seems like there is further code after the header statement since working with exit solves your issue. So that people crossing this post quickly see the solution I add this answer.
To avoid this unwanted behaviour it's a good habit to place a die or exit after the statement.
If I use a code like this:
<?php
echo 'Text';
include("Class/MyClass.php");
echo 'Text';
?>
This code only returns one 'Text'. (The one before the include)
I don't get an error (error reporting on), and the code works on my local XAMPP.
Debian 6.0 / PHP 5.3.3-7+squeeze15
Apologize in advance - it's not the answer but just wild guesses.
"Class/MyClass.php" (or any file it includes) contains somewhere exit() or die() calls that depend on configuration.
Try setting error_reporting(E_ALL) and ini_set('display_errors', '1') right before including.
I don't have access to the apache server or whatever is running on the server. I just have a free account with a web host. I do apparently have access to the .htaccess file, but I'm not sure if I feel safe enough to temper with the "nuts and bolts" as it were. So instead I tried to enable debug printouts in my php script like so:
error_reporting(E_ALL);
I'm not seeing any errors, but I know that there is an error since the entire script isn't executing.
Is there an easy way to get error printouts to show in the code that php returns? If I tried to temper with .htaccess, what would I have to do there? It's only 36 bytes long.
DO:
error_reporting(E_ALL);
ini_set('display_errors',"On");
You could also try debug_backtrace().
var_dump(debug_backtrace());
I am working on a PHP website, and I am moving it over to a new server. The new server I am moving to does not have CRON compatibility. To compensate for this I have devised a system using time formats and database tables and more to run my code instead.
What I am having a problem with is this bit of code:
if ($lasttime < $pretime)
{
$newtime = strtotime("now");
queryMysql("UPDATE time SET time=".$newtime." WHERE time=".$lasttime);
include_once 'grabber/grabber.php';
}
Specifically it's the include_once 'grabber/grabber.php'; which is causing the problem. When the timer comes round and this code runs, it gets to the include and then the code stops, with no error provided, so the include fails. I have tried changing it to an exec() but to be honest I don't completely understand how exec() works and if it is the correct thing to do. This is how I used it:
if ($lasttime < $pretime)
{
$newtime = strtotime("now");
queryMysql("UPDATE time SET time=".$newtime." WHERE time=".$lasttime);
$grabber = $base."grabber/grabber.php";
exec($grabber);
}
This does not stop the code and seems to run but it doesn't actually work, if grabber/grabber.php runs correctly then I get an email to confirm using the PHP mail() function
If anyone could help me solve this or shed some light that would be brilliant.
Thanks.
This is most probably an issue with the file location or permissions. There should be some kind of error, or the code doesn't stop, but you don't properly check that or there is some kind of an issue with the code in grabber.php itself. Add some debugging lines - print the filename, so you can check for errors in the path/name; add error_reporting(E_ALL); ini_set('display_errors', true); somewhere above the include_once line; make sure the file is where you're trying to open it from, taking into account relative paths, etc. Make sure you have permissions to run this file.
exec() is not what you need in this case, at least not in the way that you're trying to use it.
If that doesn't help - give some more information about how you run the scripts that you've shown, what's in the grabber.php file, what errors you get, etc.
(Assuming your server is *nix) If you want to use exec() you need to place a hashbang at the top of the script that points to the PHP executable and give it execute permissions.
Or (this is probably the better/more portable approach), change
$grabber = $base."grabber/grabber.php";
exec($grabber);
to
$grabber = "php ".$base."grabber/grabber.php";
exec($grabber);
...as if you were running it from a terminal.
However, I doubt this will solve the problem - I think the answer is more likely to be one of these things:
A parse error in grabber.php. Keep in mind that there are slight syntax differences between major PHP versions - if your PHP version is different on your old/new hosts, this may be the problem.
A call to a function that was defined on your old host but not on your new host, because of a difference in PHP version or installed extensions
grabber.php was corrupted during the move between servers
Try it with the include_once, but do ini_set('display_errors',1); error_reporting(-1); to make sure you actually see any errors. How are you calling you main script? How will you see the errors? Edit the question with this info, any code from grabber.php you think may be relevant and I will expand this answer.
I can't figure out why this won't work.
$docRoot = getenv("DOCUMENT_ROOT");
include_once($docRoot."/conn/connection.php");
include_once($docRoot."/auth/user.php");
It works locally through wamp, but it won't work on my live server. I tried this:
if(!include_once($docRoot."/auth/user.php")){
session_start();
$debug = array();
$debug["docRoot"] = $docRoot;
$debug["inc_path"] = $docRoot."/auth/user.php";
$debug["file_exists"] = file_exists($debug["inc_path"]);
$_SESSION['DEBUG'] = $debug;
// exit
header("Location:debug.php");
exit();
}
The debug page just echoes that array and it shows the correct absolute paths and indicates that the file does in fact exist. So why didn't the include_once() work? The server (a DV account on a MediaTemple server) has not been configured at all, so I wonder if there is an apache or php setting that is messing with me.
Ultimately, what I want here is a way to refer to a file in such a way that if I move the file, or include it in another file, nothing will break. Any ideas?
In your debugging you might try is_readable($docRoot."/conn/connection.php"). The file_exists function will return true even if the file does not have readable permissions.
If you get an error code we might be able to provide more info as to what is going wrong.
Turn on error reporting dummy. It turns out one of the includes on another file was breaking this page and I wasn't able to trace that out until I turned on error reporting.
Incidentally, the problematic include was missing a leading slash in the filepath ( include("dir/file.ext"); ) which works on my local wamp setup and breaks on the linux server.