<?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">
开发者_Go百科 <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 ??
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" />
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
You need to exit; after you send Location header. Otherwise the redirect will not happen.
header("Location:".$var.".html");
exit;
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 withinclude()
, orrequire()
, functions, or another file access function, and have spaces or empty lines that are output beforeheader()
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.
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.
You can check if form is sent by
if($_SERVER['REQUEST_METHOD'] == 'POST') {
test();
}
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">
精彩评论