need help in PHP coding - double header not working if used twice??

online41

New Member
Messages
2
Reaction score
0
Points
0
hello.. im just a newbie here, and also in PHP coding..

I'm having a problem with header syntax like this..
Code:
header("location:mysite.php");

everytime i used this in a single PHP page twice,it doesn't redirect the page..:frown:

can someone help me about header syntax?maybe i just didn't used the code right..
i really appreciate the help..

thanks!
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
That's not enough information to reproduce the problem. Create a minimal test case, post the code here and include a link to a live version of the minimal test case.

For more on the syntax of the "Location" header, check the current HTTP standard. Note that though the standard calls for an absolute URI, many browsers support redirects to relative URIs.
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Does it work if you only use it once?

Why are you doing it twice? You should exit as soon as you use it the first time.
 

online41

New Member
Messages
2
Reaction score
0
Points
0
That's not enough information to reproduce the problem. Create a minimal test case, post the code here and include a link to a live version of the minimal test case.

For more on the syntax of the "Location" header, check the current HTTP standard. Note that though the standard calls for an absolute URI, many browsers support redirects to relative URIs.


i've already changed my codes and used the header once.. but it still not redirecting to the location i put in the header... it still stays on the same page.. BTW i'm creating an online examination.. here's my code..

PHP:
<?php
session_start();
include("config.php");
include("overall_assess.php");
include("course_specialty_function.php");
include("exam_assess_function.php");
$examid = $_SESSION['examid'];
$exam_score = $_SESSION['exam_score'];
$item_count = $_SESSION['item_count'];
$arith_score = $_SESSION['arith_score'];
$cler_score = $_SESSION['cler_score'];
$log_score= $_SESSION['log_score'];
$com_score = $_SESSION['com_score'];

$score_array = array($arith_score,$cler_score,$com_score,$log_score,);

$result = mysql_query("SELECT * FROM exam_result_tb WHERE username = '$_SESSION[username]'");
$count=mysql_num_rows($result);

$exam_no = $count;
if ($exam_score>$texam_good){
echo "Excellent";
$remark = "Excellent";
}
elseif ($exam_score>$texam_average&&$exam_score<=$texam_good){
echo "Good";
$remark = "Good";
}
elseif ($exam_score>$texam_fair&&$exam_score<=$texam_average){
echo "Average";
$remark = "Average";
}
elseif ($exam_score>$texam_poor&&$exam_score<=$texam_fair){
echo "Fair";
$remark = "Fair";
}
elseif ($exam_score>=0&&$exam_score<=$texam_poor){
echo "Poor";
$remark = "Poor";
}

include("config.php");
foreach($score_array as $each_remark){
if ($each_remark>=$logpass){
$score_remark[]="Good";}
elseif($each_remark<$logpass && $each_remark>=$logave){
$score_remark[]="Average";}
elseif($logave>$each_mark && $each_remark>="0"){
$score_remark[] = "Failed";}
}


$s_time = mysql_query("SELECT curtime FROM exam_result_tb WHERE examid = '$examid'");
$row = mysql_fetch_array($s_time);
$curtime = $row['curtime'];
$time_end  = time();
$time_taken = $time_end - $curtime;
$time_took_min = "";
$time_took_sec = "";

$time_took_min = floor($time_taken/60);
$time_took_sec = ($time_taken`);


$tot_time_take = $time_took_min." minute(s),".$time_took_sec." second(s)";


    mysql_query("UPDATE exam_result_tb SET  time_taken='$tot_time_take',finish_time='$time_end',cler_score='$_SESSION[cler_score]',arith_score='$_SESSION[arith_score]',log_score='$_SESSION[log_score]',com_score='$_SESSION[com_score]',exam_score  = '$_SESSION[exam_score]', t_exam = '$texam' , remark =  '$remark',exam_no = '$exam_no' WHERE examid = '$examid'");
$username=$_SESSION['username'];

session_destroy();
session_start();
$_SESSION['time_end'] = $time_taken;
$_SESSION['qualification'] = $ave_cat_remarks;
$_SESSION['tot_time_take'] = $tot_time_take;
$_SESSION['score_array'] = $score_array;
$_SESSION['score_remark'] = $score_remark;
$_SESSION['texam'] = $texam;
$_SESSION['username'] = $username;
$_SESSION['exam_remark'] = $remark;
$_SESSION['exam_score'] = $exam_score;   
$filename = "demo3.png";
unlink($filename);
header("Location:onlineitexam.x10.mx/online_ex_php/exam_result.php");
?>


any help and advice would be great.. thanks
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Do you get an error with your code?

You should not output anything other than the header.
 

revcent

New Member
Messages
5
Reaction score
0
Points
0
Removing the apostrophe in the code below might solve your problem.

PHP:
$time_took_sec = ($time_taken`);
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
That's hardly a minimal test case. Read the link in my previous post (it's there for a reason).

PHP:
header("Location:onlineitexam.x10.mx/online_ex_php/exam_result.php");
That's likely not URL for a resource on your server. URLs are absolute, in which case they begin with a scheme (e.g. "http:"), or are relative, which is a path relative to the current URL (if there's no leading slash) or to the site root (if there is a leading slash). A few browsers may be able to understand URLs of the form "//www.example.com/path/to/resource", but you shouldn't depend on it. The URL you're setting is relative; the "onlineitexam.x10.mx" will be interpreted as a path component rather than the server name, and is relative to the location of the current page. The pages I previously linked to already cover this.

Code should be indented for readability.

Don't use SELECT * unless you're writing a DB administration program; select only the columns you need.

Database access and application logic should be separated into distinct modules. Look up architectural patterns such as MVC and multi-tiered architectures (3-tier is common).

The sample code may be vulnerable to SQL injection, which is a very serious security risk. To fix this hole, switch from the outdated (and soon-to-be deprecated) mysql extension to PDO and use prepared statements. Data shouldn't be interpolated directly into statements. If you need a PDO tutorial, try "Writing MySQL Scripts with PHP and PDO". The site you save may just be your own.
 
Top