0
<?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
  • This might be obvious to more seasoned php developers, but what's the problem? What (if anything) is happening, or not happening? Commented Oct 18, 2010 at 19:45
  • when i click "TAke test"..nothing happens the same page is displayed... Commented Oct 18, 2010 at 19:47

7 Answers 7

6

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" />
Sign up to request clarification or add additional context in comments.

Comments

3

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

2 Comments

or just header("Location:$var.html"); or for clarity: header("Location:{$var}.html");
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 & ~E_NOTICE); at the top of your file to make sure.
2

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

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

Comments

2

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.

8 Comments

If he had PHP warnings turned on he should get a warning message about that.
how can i implement this without using the header function??
@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.
@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.
@Vinod K Perhaps you have some sort of output buffering? This works using output control functions. If you modify headers before calling ob_flush(), 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.
|
1

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.

1 Comment

Did you try to put declaration of test function before its call?
0

You can check if form is sent by

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

Comments

0

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">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.