0

I was trying to redirect an html page to two different php files. Html file which i created is redirect.html. When i go to that page it would check if i was logged in or not, if i was not it would take me to ucplogin.php on the other hand if i was logged in it'd take me to ucphome.php. Here the problem is the code i'm trying is obviously messed up because i've been trying for a while and still doesnt work

<?php
session_start();
?>

<!DOCTYPE HTML>
<html lang="en-US">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="refresh" content="1;url=http://example.com">
        <script type="text/javascript">


            <?php
            if(!isset($_SESSION['playername']))
            {
            ?>
            window.location.href = "http://localhost/working/ucplogin.php"
            <?php
            }
            else
            {
            ?>
            window.location.href = "http://localhost/working/ucphome.php"
            <?php
            }
            ?>

        </script>
        <title>Page Redirection</title>
    </head>
</html>

EDIT: I have seen all those answers and got some points but i rewrite the code to this

 <?php
session_start();
?>

<?php
    if(!isset($_SESSION['playername']))
    {
    ?>
    header('Location: http://localhost/working/ucplogin.php');
    <?php
    }
    else
    {
    ?>
    header('Location: http://localhost/working/ucphome.php');
    <?php
    }
?>

when i go to redirect.php it gives me header('Location: http://localhost/working/ucphome.php'); when i'm logged in and this when i'm not header('Location: http://localhost/working/ucplogin.php');

Apparently it works but technically not, it doesn't redirect me to the exact page.

Prabin
  • 33
  • 7

2 Answers2

1

Html file which i created is redirect.html

You cannot run PHP code in a plain HTML file, unless you instruct Apache to run HTML files as PHP (Thanks to @Fred -ii- for pointing this out. Also see @Peyman.H answer for how to do this). The reason the php extension exists is to tell the server there is PHP code that needs to be processed. Rename the extension to .php and you will be good.

Additionally, what if the user has JavaScript disabled? It is safer to use the PHP function header() for your redirects instead:

if(!isset($_SESSION['playername'])) {
    header('Location: http://localhost/working/ucplogin.php');
} // etc...
samrap
  • 5,245
  • 5
  • 29
  • 55
  • 1
    *"You cannot run PHP code in a plain HTML file."* - that's not entirely true. You can if instructing Apache to treat `.html` files as PHP. – Funk Forty Niner Sep 09 '15 at 22:28
  • but header is not working in my code, it just show the line in the page when i go to redirect.php, i have edited the code above,, – Prabin Sep 09 '15 at 23:03
  • No, header is a _PHP_ function, you need to place that code in your PHP. – samrap Sep 09 '15 at 23:05
0

You can also use in your file a JavaScript code by just typing the following

  <script>
window.location="redirect.php" ;
    </script>