header / redirect not work in host

hello
12 Replies
NIMA
NIMAOP4w ago
every time i bulid a website with php and i put it in host some of header and redirect wont work even if name are fix but in localhost are fine what should i do do fix this problems and how detect it beffor puting it in website like in this part https://nimakhadivar.ir/nima/main/dashboard/result.php it should redirect user to another page but it will stuck and dont do anything
<?php
// total exam
$sql = "SELECT COUNT(*) as exam_id FROM result_more WHERE uid = '$user_id'";
$result = mysqli_query($conn, $sql);
$count = mysqli_fetch_assoc($result)['exam_id'];
$tedad_exams_toal = $count ;


if( $tedad_exams_toal == 0){
header("Location: exam.php");
exit();
}

?>
<?php
<?php
// total exam
$sql = "SELECT COUNT(*) as exam_id FROM result_more WHERE uid = '$user_id'";
$result = mysqli_query($conn, $sql);
$count = mysqli_fetch_assoc($result)['exam_id'];
$tedad_exams_toal = $count ;


if( $tedad_exams_toal == 0){
header("Location: exam.php");
exit();
}

?>
<?php
ἔρως
ἔρως4w ago
you should install xdebug and the xdebug extension in your ide/editor
Jochem
Jochem4w ago
is that everything in the file? Nothing above the <?php? Not even a space or a newline? no errors in the log for the mysql connection? cause if that fatal-errors out that header line is never going to run I'm guessing there is stuff, cause $user_id isn't defined anywhere here
ἔρως
ἔρως4w ago
this does stink to "utf-8 bom" or just bad code this only redirects if there's nothing in the database but the way it's written is very hard to follow
Jochem
Jochem4w ago
yeah, the variable names aren't great it should be exam_count or exam_total and the as should match the variable name in PHP or number_of_exams would work too
ἔρως
ἔρως4w ago
and it is receiving potential user data without prepared statements
Jochem
Jochem4w ago
which is excusable if the $user_id comes from a fully trusted source (like a prepared statement earlier), or a session in a pinch, but not from any of the superglobals
ἔρως
ἔρως4w ago
we actually don't know it's possible the bug is there if there's a bug where the user id is always set to the same value, this will always return the same result even when the user logged in is different
Jochem
Jochem4w ago
I'm half-suspecting "local" meaning "PHP Built-in server" and hosted is apache/nginx with php-fpm or something, and they handle headers just differently enough for a blank line before <?php to not be an issue in one and an issue in the other but yeah, check the error log, if that doesn't work, install xdebug
ἔρως
ἔρως4w ago
oh, yeah, apache flips as soon as you send a single character and then call header()
Jochem
Jochem4w ago
or just turn on show_errors
ἔρως
ἔρως4w ago
and restart the server when you do it
Want results from more Discord servers?
Add your server