Pergunta

G'day,

This is for my tutorial purpose.

I have 3 files

1. mlogin.htm - Takes the input from the user (login name and password). The action is set to the next file so the details can be checked.

<form id="logIn" name="logIn" method="get" action="mlogin.php"> 

2. mlogin.php - Takes the value from mlogin.htm using GET method. If the details match the details in XML file, the user is redirected to the next file

$musername = $_GET['username'];
$mpassword = $_GET['password'];
exit(header('Refresh:5; url=mloginsuccess.htm'));

3. mloginsuccess.htm - Displays the menu.

Now, what I'm trying to do is to show the username in the 3rd file so it's something like

Welcome, John

I do realise that I can do this using a session by changing the 3rd file to a

mloginsuccess.php 

but it MUST be a

mloginsuccess.htm

I was wondering if this is possible.

Any help is appreciated :)

Foi útil?

Solução

Suppose for a moment that you actually do want to follow your instructions to the letter. (You don't really want to do this, probably... interpreting requirements, rather than following them exactly, is a key trait of a decent software engineer.) If your requirement is that you must use a static page, you have a couple options for getting data accessible on that page. All of which require JavaScript.

  1. Cookies
  2. Query String
  3. Anchor Fragment

Basically, you need to set this data in one of these three places so that you can access it with JavaScript from your static HTML page later on. To set a cookie with PHP, use setcookie(). To read it with JavaScript, use document.cookie, or one of the many snippets of code to make this easier.

To set the query string, simply do so in your redirect:

header('Location: http://www.example.com/mloginsuccess.htm?name=' . urlencode($_GET['username']));

See this question for the JavaScript needed to read the query string: How to get the value from the GET parameters?

Finally, for the anchor fragment, you can often redirect to it the same way. (However note that not all browsers are guaranteed to follow the anchor fragment part of the URL!) To read the anchor fragment, use window.location.hash.

I hope that in the end, you will choose to do none of these and keep your auth logic in a sensible place. Literal interpretation of requirements rarely leads to good code and application design. At a minimum, you can hack around the URL requirement with a rewrite rule, making whatever.html be an alias to whatever.php. The client doesn't know or care what is actually running on the server... that's the server's job. I would tell you how to write a rewrite rule, but you didn't specify which server you are using, so I'll leave that part up to you to Google.

Outras dicas

How can you expect to use a php feature(SESSION) in a file which is not php(.HTML). However you are allowed to use html inside a php file as php is a template engine and process the html ...refer this for for indepth What renders the HTML?

just convert your .html to .php and

<?php>
session_start();
$_SESSION['username']=$_GET['username']
?>

<html>....<body>welcome <?=$_SESSION['username']?></body>...</html>

or however your html tags are.

Maybe you can use AJAX to load session details. For example, using JQuery,

<script>
...
$(document).ready(function(){
    $.ajax({
        url: "load_session.php",
        success: function(uname){
            $("#uname").html(uname);
        }
    });
});
...
</script>
...
Welcome, <span id="uname"></span>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top