tags:

views:

94

answers:

7
   <?php
(E_ALL & ~E_NOTICE);

session_start();

// is the one accessing this page logged in or not?
if (!isset($_SESSION['db_is_logged_in'])
   || $_SESSION['db_is_logged_in'] !== true) {

   // not logged in, move to login page
   header('Location: login.php');
   exit;
}
else{
        echo "Welcome To The Test Page:";
        echo $_SESSION['logname'];

}
if(isset($_POST['submit'])) {
test();
}

function test(){
$var=rand(1,5);
header("Location:{$var}.html");
exit;

}

<html>
<body>
<form action="<?=$_SERVER['PHP_SELF'];?>"  method="post">
<input type="submit" name="submit" value="Take Test">
</form>
</body>
</html>

When the "take test " button is clicked i need 1 of the 5 html pages i.e (Question papers in this case ) to be displayed to the user. for that i have created the random number from 1 to 5. The pages have been names 1.html , 2.html and so on...

Could anyone debug this code ??

+2  A: 

You have extra quotes in

header('"Location:".$var.".html"');

It should be

header('Location:'.$var.'.html');

or if you prefer double quotes as mentioned in the comment to this answer you can use the php double quote variable interpolation:

header("Location: {$var}.html");

The string your would set the header to would be:

"Location:".$var.".html"

instead of what you actually wanted

GWW
or just `header("Location:$var.html");` or for clarity: `header("Location:{$var}.html");`
Peter Ajtai
still the same....the same page is displayed..
Vinod K
You also have other issues in your code that other people have mentioned in their answers. You should make sure you have warnings set in your php error reporting. You could put error_reporting(E_ALL at the top of your file to make sure.
GWW
+2  A: 

You need to exit; after you send Location header. Otherwise the redirect will not happen.

header("Location:".$var.".html");
exit;
Anti Veeranna
+2  A: 

You can't call header() after your wrote something in your page.

Here, when you're successfully logged on, you echo a message and then try to change the header. But it's too late.

From the php doc :

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.

If you want instead to include some page content, then include it, don't change the Location in the header.

Plus, as others pointed out, your string for the header is wrong, "Location:".$var.".html" will be enough.

Colin Hebert
If he had PHP warnings turned on he should get a warning message about that.
GWW
@GWW even an error here.
Colin Hebert
how can i implement this without using the header function??
Vinod K
@Vinod K, if you have a header and you want a redirection, then you shouldn't write anything. If you simply want to include the content of another page, then include it and you can keep the first `echo`, it depends on what you want.
Colin Hebert
He could also use output buffering http://php.net/manual/en/book.outcontrol.php
GWW
@Vinod K Depending on your intention, you could simply `include` the page and have it produce the desired output. If you really want to redirect the browser to another page, `header("Location: {$var}.html")` is fine, but you just have to ensure that you haven't produced any output before doing so.
Paul Lammertsma
Hey....the codes working ...i added exit after the header...and added the name="submit" in the form...its working currently....Thnx a lot for ur inputs..
Vinod K
@Paul Lammertsma i generate a echo statement in the beginning of the page welcoming the user... is that considered as an output..if so...how come this code works now??
Vinod K
@Vinod K Perhaps you have some sort of output buffering? This works using [output control functions](http://nl.php.net/manual/en/ref.outcontrol.php). If you modify headers before calling [`ob_flush()`](http://nl.php.net/manual/en/function.ob-flush.php), then nothing's the matter. If you are using some sort of templating engine, this might be why you're not getting any errors. Otherwise, I'm stumped.
Paul Lammertsma
hey ...i edited the initial code with the working one...
Vinod K
@Vinod K, well I agree with @Paul here, either you have OB functions somewhere, or you're using a templating engine. I don't see anything else.
Colin Hebert
+1  A: 

It appears you declared test function after the call, so you must it's better practice to move it before the call, I would put it before session_start();

You also have to avoid output, in order to get header to do what you want. You can do this by putting an exit(); after the header instruction.

Also you have extra quotes as GWW said in his answer.

Update

Well, I didn't notice this before, in PHP you can call functions before the declaration, although this is currently confusing me a little.

Minkiele
i did change the code...added exit but still the same...
Vinod K
Did you try to put declaration of `test` function before its call?
Minkiele
a combination of ur and others suggestion worked!!!!...thnx
Vinod K
+6  A: 

Other comments have addressed two problems: output before changing headers, and the invalid formatting around the quotes.

Another problem is that $_POST['submit'] is never specified from the HTML. You have:

<input type="submit" value="Take Test" />

But nowhere is a name for the field specified. This should read:

<input type="submit" name="submit" value="Take Test" />
Paul Lammertsma
Thnx a lot!!!!!!! it worked...
Vinod K
A: 

You can check if form is sent by

if($_SERVER['REQUEST_METHOD'] == 'POST') {
test();
}
noxvile
A: 

try pointing the form to the page manually instead of using the server var.

from

<form action="<?=$_SERVER['PHP_SELF'];?>"  method="post">  

to

<form action="test.php"  method="post">

or even

<form action="<? echo $_SERVER['PHP_SELF'];?>"  method="post">
Brian Perin