-1

Edit: Thank you for your help and time. I will change all my extension to .php as recommended and check the link that was shared :)!

I'm a beginner at coding and I have been stuck for a few days on a problem. So, I have coded a html page with a login form. Once the user click the button login, the username and password will be checked in a php file and then if it's correct, the user will be redirected to the successfully login html page. I get redirected to the success login html page but I just can't pass the user data, I have tried to add a .htaccess so I can try to echo the session username but it doesn't display the username.

How do I pass the data of the user session to the new html page? I have been looking on forum and online but didn't really find an answer. If you guys have advices or suggestions, it would be helpful as I'm a beginner. Thanks for your answers and help!

Here is the php part of what should happen if the user successfully login:

check.php

if(password_verify($password, $row["password"]) {  
    $_SESSION['loggedin'] = true;
    $_SESSION['username'] = $username;
    header( 'Location: http://successfullogin.html' );
} 

The successfullogin.html code:

<?php 
session_start()
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Profile</title>
 <meta name="viewport" content="width=device-width, initial-scale=1, 
shrink-to-fit=no">
<link rel="stylesheet" href="/css/style.css">
<script src="/js/script.js"></script>
 </head>
  <body>

  <div>

<h1>Sucess login </h1>
<?php
echo $_SESSION[‘username’];

?>

</div>

</body>

</html>
user14144054
  • 3
  • 1
  • 3
  • `Everything works but I just can't pass the user data`??? – GetSet Aug 23 '20 at 18:33
  • 1
    Everything doesn't work apparently. Do you have `session_start()` at the very top of your pages, before anything else? – GetSet Aug 23 '20 at 18:34
  • @GetSet I have a session_start() at the very beginning of my page. I mean it redirect me to the header location when the username and password are correct. Sorry for my wordings. – user14144054 Aug 23 '20 at 18:37
  • 2
    Does the redirect page have the session_start(). Can you show that code? – GetSet Aug 23 '20 at 18:38
  • @GetSet sure I will edit my post with the new page. Thanks! – user14144054 Aug 23 '20 at 18:42
  • @GetSet It's very simple as I first wanted to be able to get the data before adding anything else – user14144054 Aug 23 '20 at 19:00
  • You need `session_start()` first thing on all pages (just once though, on the top-most page, not every included page) that deal with both setting and using the session so whereever the `if(password_verify($password, $row["password"])` line is, that also requires `session_start()`. – Rasclatt Aug 23 '20 at 19:00
  • Answer by @Aggestor Mhl may be applicable to your problem if your redirect file is not extension'd in `.php`. – GetSet Aug 23 '20 at 19:47
  • @GetSet Yes, thanks! I will change all my extension :) – user14144054 Aug 23 '20 at 21:59
  • @Rasclatt Thanks I will keep it in mind and put it at the top of my php file each time! – user14144054 Aug 23 '20 at 21:59

1 Answers1

0

You can't run PHP in .html files cause the server don't recognize that extension as a valid one. To tell your server to understand html files as valid check this topic below

How do I add PHP code/file to HTML(.html) files?

Or change your successfullogin.html to successfullogin.php

Aggestor
  • 55
  • 5
  • Makes sense. OP says that it does get redirected. Good point to mention that the redirect file should also extension in `.php`. – GetSet Aug 23 '20 at 19:46
  • Ok thanks, this was one of the option I was thinking about. I just wanted to see if there was another way to pass the data while keeping the .html extension. I saw some people mention ajax too, but I saw most example were on php file extension too. – user14144054 Aug 23 '20 at 19:58
  • Ajax needs a PHP file which will talk to your server, so you just make an ajax call using Javascript to display ajax response in an HTML file – Aggestor Aug 23 '20 at 20:01
  • 1
    Basically ajax helps to get data from server without reloading the whole document, I'll recommend you to learn it if you never learn it before – Aggestor Aug 23 '20 at 20:03
  • @AggestorMhl Ok thanks for recommendation! I think I should start from that too :)! – user14144054 Aug 23 '20 at 21:49