Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML PHP Display username after success login

Tags:

html

php

I have a <div> inside my onlinestore.html file which is my menu that contain of Login/Register. What I want is after the success login, the <div> for login/register change to the username. What i'hv done wont display the expected output that i want.So is there something wrong about my code?

Here is what I've done:

onlinestore.html

<li class='active' style='float:right;'>
  <?php 
  session_start();
    if($_SESSION['logged']==true){ 
        echo $_SESSION["username"];
        echo '<a href="logout.php"><span>Logout</span></a></li>';
        }
    elseif($_SESSION['logged']==false) 
        echo '<a href="registerform.html"><span>Login/Register</span></a></li>';
    ?>

Here is another file checklogin.php:

if($count==1){
  session_start();
  $_SESSION['logged']=true;
  $_SESSION ['username']=$myusername;
  header("refresh:1;url=onlinestore.html");
  }
else{
   $_SESSION['logged']=false;
   header("refresh:2;url=login.html");}

Here is the expected output:

Before Login enter image description here

After Login enter image description here

Here is what i get with the code above: My code wont work

like image 302
JK9 Avatar asked Dec 18 '13 09:12

JK9


2 Answers

Either you need to rename your onlinestore.html and login.html to be .php files so the PHP will work in them, or use the addtype option in your htaccess file.

In your onlinestore.html do this:

<?php
session_start(); // Right at the top of your script
?>

<li class='active' style='float:right;'>
  <?php 
  if($_SESSION['logged']==true)
    { 
      echo $_SESSION["username"];
      echo '<a href="logout.php"><span>Logout</span></a></li>';
    }
  elseif($_SESSION['logged']==false)
    {
      echo '<a href="registerform.html"><span>Login/Register</span></a></li>';
    }
  ?>

In your checklogin.php do this:

<?php
session_start(); // Right at the top of your script
?>

if($count==1)
  {
    $_SESSION['logged']=true;
    $_SESSION['username']=$myusername;
    header("Location: onlinestore.html");
    exit();
  }
else
  {
     $_SESSION['logged']=false;
     header("Location: login.html");
     exit();
  }

If the above doesn't work then please tell me what happens.
Do you have html files set to parse PHP code?
Or a htaccess file with:
AddType application/x-httpd-php .htm .html
?

EDIT

Try this for debugging:

In your checklogin.php do this:

<?php
session_start(); // Right at the top of your script
?>

if($count==1)
  {
    $_SESSION['logged']=true;
    $_SESSION['username']=$myusername;
    echo "Login worked";
    exit();
  }
else
  {
     $_SESSION['logged']=false;
     echo "Login failed";
     exit();
  }

This will show you if login is working. If it's not and you get "login failed" then that is why you get the "Login/Register" link on your page.

If it shows "Login worked" then set the code back to how it was and then on your onlinestore.html page, do this at the top of the file:

echo "This is working";
exit();

What happens? Do you get the message "This is working" on the page and nothing else?

like image 114
James Avatar answered Sep 28 '22 18:09

James


In your else statement you haven't defined a session_start() like you did in your if statement.

And else, instead of checking the value of a $_SESSION and determining the value of it, you can use the following:

if (session_status() == PHP_SESSION_NONE) {
//Do something if the session is not existing
}

Other options are storing variables in a $_COOKIE and then check if it isset or not(if(isset($_COOKIE["username"]){})

I hope this has helped you out

like image 28
Orion Avatar answered Sep 28 '22 18:09

Orion