0

I am trying to set up an exam page for my friend Neil. The user clicks on a link 'First Year exam' or 'Second Year exam' and goes to a login page. If the login is successful, and the time is right, the exam opens

I use this to make the exam page available at a certain time:

<?php
$time = date('Y:F:d:G:i');
echo 'the time now is ' . $time . '<br>';
// the page is only available between 10 and 11
if(date('G') < 10 or  date('G') > 10  )  {
    include 'jsclock.html';
   exit();
    }
?>

Works OK!

Now I would like to use PHP to open jsclock.html at 11 o'clock. I tried this at the top of the exam page:

echo "Welcome to the exam page</br>"; 
echo "we will redirect to the jsclock.html webpage in 30 seconds"; 

// The function will redirect to jsclock.html in 30 seconds
header("refresh: 30; url = jsclock.html"); 
exit();

But I get this message:

Welcome to the exam page we will redirect to the jsclock.html webpage in 30 seconds Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/Neil_Exam/FirstYear/19BEsW13.html.php:4) in /var/www/html/Neil_Exam/FirstYear/19BEsW13.html.php on line 7

The login.php will direct the user to the exam page if the login is successful:

//validate inputted password with $user password
                if(password_verify($password, $user['password'])){
                    //action after a successful login
                    //for now just message a successful login
                    $_SESSION['success'] = 'User verification successful';
                    //header('location: 19BEsW13.html.php');
                    include '19BEsW13.html.php';
                    exit();

The login part also works well!

I do not use

'header('location: '19BEsW13.html.php');'

in login.php because I keep getting weird errors when I use

header('location ..

anywhere, so I use include statements.

Any tips on how to overcome this error please??

I could use javascript. I was told that it is 'trivial' to alter a webpage to change javascript settings. (Although I have no idea how!!) So I thought it is better to use php if I can.

Pedroski
  • 433
  • 1
  • 7
  • 16
  • PHP is server side. Once you output to the client you will need to use client side language, such as JS. – user3783243 May 25 '20 at 04:00
  • So I couldn't use php to open my exam page, then redirect after say 3600 seconds?? I got rid of the comments, now I get a blank page, and after 30 seconds it goes to jsclock.html Suppose I will have to use javascript!! I have a nice little script to do that, it is just, I was told javascript is easy to manipulate, if you know how, which I don't! Thanks! – Pedroski May 25 '20 at 05:54

2 Answers2

1

You cannot echo or output anything before using header(). Check out the documentation: https://www.php.net/manual/en/function.header.php

Adam Winter
  • 1,680
  • 1
  • 12
  • 26
1

Like @Adam Winter said you cannot use header() if in your code you already echo or output some data.

If you still want to redirect the user you can use Javascript to do so like that :

echo '<script type="text/javascript">setTimeout(function(){ window.location.replace("jsclock.html") }, 30000);</script>';
user3783243
  • 5,368
  • 5
  • 22
  • 41
MehdiDev
  • 31
  • 3