7

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

JK9
  • 340
  • 2
  • 5
  • 23
  • 2
    Yeah you ask for something that you already done. – David 'mArm' Ansermot Dec 18 '13 at 10:00
  • the code i show above is not working like the expected output.Is there anything wrong in my code? – JK9 Dec 18 '13 at 10:00
  • You should use the $_SESSION["username"] and put it in that div/span or whatever container that you are using. – rccoros Dec 18 '13 at 10:00
  • 1
    As I said in the other question, you need to say WHAT doesn't work. You get a blank page? User never seems to be logged in? etc – James Dec 18 '13 at 10:00
  • where is the login script? where you set the $_SESSION attributes. – q0re Dec 18 '13 at 10:01
  • @James i'hv edit the question and show the screenshot of the error i get – JK9 Dec 18 '13 at 10:05
  • Looks like you're not setting the $_SESSION['username'] properly (or at all). Can you `var_dump($_SESSION['username'])` and see the real output? If it's null, that's your problem – casraf Dec 18 '13 at 10:05
  • possible duplicate of [HTML Change login label to username after login](http://stackoverflow.com/questions/20611949/html-change-login-label-to-username-after-login) – Glavić Dec 18 '13 at 16:14

8 Answers8

2

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

Orion
  • 227
  • 1
  • 4
  • 13
2

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?

James
  • 4,317
  • 4
  • 33
  • 45
  • it doesn't work,it still show me the same output that i show above – JK9 Dec 18 '13 at 10:53
  • Again, what doesn't work? The image you show has "Login/Register" and looks like a link, which looks to me like the code in `onlinestore.html` is going to the else. I'll edit my answer with some things to try – James Dec 18 '13 at 10:56
  • the problem is just exactly same as the question.Nothing change with the code you provide above – JK9 Dec 18 '13 at 10:58
  • i get the message Login worked,but my onlinestore.html got nothing change,it is just like the screenshot i provided. – JK9 Dec 18 '13 at 11:07
  • And the second test I stated to use if the login returned ok? `echo "This is working";exit();` - does this work on onlinestore.html? – James Dec 18 '13 at 11:09
  • where should i copy this to my onlinestore.html? before echo $_SESSION['username']? – JK9 Dec 18 '13 at 11:12
  • As I said in the answer, at the very top of the file. Just want to check the echo works ok. Then let me know if you get a white page with just "This is working" or not. – James Dec 18 '13 at 11:16
  • It's only show login worked,but the this is working is not shown – JK9 Dec 18 '13 at 11:18
  • You really need to read all the advice given. I said here and in the other answer that `.html` files don't parse PHP by default. You need to either change your file extensions whenever you want PHP to work in them (eg change `onlinestore.html` to `onlinestore.php`) or use the other approach of making PHP work within html files: http://stackoverflow.com/questions/11312316/how-do-i-add-php-code-to-html-files#11312349 – James Dec 18 '13 at 11:23
  • In fact, if the site is not live or too large already, and if you're going to be using PHP here and there (as you are starting to already), it might be worth considering changing all your files and internal links to .php. Doing it now while you're developing the site is much better than having to attack this once it's live and too late to make it a simple job. – James Dec 18 '13 at 11:28
0

Well, there is a lot of code missing.

Where is your login script?

First, you need a <form> with the input fields (login and password) and a submit button.

For example:

<form action='login.php' method='post'>
<input type='text' name='loginname'></input>
<input type='password' name='loginpassword'></input>
<input type='submit'>Submit</input>
</form>

Your login.php

<?php 
session_start();
// Dont forget to check your variables
$loginname = $_POST["loginname"];
$password = $_POST["loginpassword"];
// Check the correct login (for example with a database)
if ($login_correct) {
    $_SESSION["username"] = $loginname;
    $_SESSION["logged"] = true;
    header("index.html");
    exit;
else {
    $_SESSION["logged"] = false;
    header("registerform.html");
    exit;


   }
}
?>

In your index.html you can do:

<?php 
session_start();
if($_SESSION['logged'] == true){ 
    echo $_SESSION["username"];
    echo '<a href="logout.php"><span>Logout</span></a></li>';
}
else {
    echo '<a href="registerform.html"><span>Login/Register</span></a></li>';
}
?>
q0re
  • 1,365
  • 17
  • 32
  • i do have this part but it's quite long,so i just skip that part – JK9 Dec 18 '13 at 10:14
  • show us the code where you set the session. I think the session is empty. You can check which session is set with `var_dump($_SESSION)`. – q0re Dec 18 '13 at 10:16
  • Maybe the `$myusername` is empty. Where you set this variable? – q0re Dec 18 '13 at 10:19
  • $myusername=$_POST["myusername"]; $mypassword=$_POST["mypassword"]; – JK9 Dec 18 '13 at 10:21
  • and the name attribute from the '' elements is also set to `myusername` and `mypassword` ? (`` and same for `mypassword`) – q0re Dec 18 '13 at 10:32
  • 1
    Have you also delete the space between `$_SESSION [` in `$_SESSION ['username']=$myusername;` ? Like: `$_SESSION['username']=$myusername;` – q0re Dec 18 '13 at 10:38
  • checked,there is no space between them – JK9 Dec 18 '13 at 10:40
0

If you want the username to be displayed instead like in the example you need to change your code for onlinestore.html to the following:

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

I think your PHP should look something like that:

<?php 
session_start();
username = $_SESSION["username"]
if($_SESSION['logged'] == true) { 
    echo '<a href="logout.php"><span>' . username. 'Logout</span></a></li>';
    }
else {
    echo '<a href="registerform.html"><span>Login/Register</span></a></li>';
}
?>

Use either brackets in both if and else or use none. By the way you don't need an elseif if you're only checking a boolean. (there are only 2 possibilities)

SAF
  • 297
  • 4
  • 19
  • This would not work because she/he did not properly call the variable `username` it should be `$username` and nor did he/she [properly end that line in this demo @JK9 and if you use it letter for letter, then it would not work. – Denver William Dec 18 '13 at 22:59
0
<div class="msg"><?php echo $msg;?></div>
<form type="submit">
   <input type="text" name="Email">
   <input type="password" name="Password">
   <button type="submit" name="Login">Login</button>
</form>
<?php
  require 'connection.php';
  if (isset($_POST['Login'])) {
      $email = $_POST['Email'];
      $password = $_POST['Password'];

      $user = "SELECT username FROM table_name WHERE email = '".$email."' and password = '".$password."'";
      $user = mysqli_query($con, $user);
      if (mysqli_num_rows($user) > 0) {
         while ($row = mysqli_fetch_array($user)) {
               $username = $row['username'];
         }
      } else {
                $msg = "Can't Log in";
      }
    }
     echo $username;
   ?>
0

Try this. I don't how to explain but when the login is successful I created the session variable. If the session variable is not in used it will echo the Login that will link to login page otherwise if session variable is being used it will print the username

code in process of logging in

        if($row['col_userName'] == $username AND $row['col_custPass'] = $password){
             $_SESSION['session_var_user'] = $username; // I created the session variable also don't forget to put the session_start(); at the top of your code
            echo "
                    <script> 
                      window.location.href = 'index.php';
                    </script>
                ";
            }else{echo "failed";}

code in navbar

        <li class="nav-item">
            <a class = "justtextstyle" href="#">
                <?php 
                   if(!isset($_SESSION['session_variable']))
  // if(!isset($_SESSION['session_variable'])) checking if $_SESSION['session_variable' is in use; sorry idk to explain, it's just like that lol 
                   {
                       echo "<a class = 'justtextstyle' href='login.php'>Log In Here</a>";
                   }
                   else
                   {
                       echo "Hi," . $_SESSION['session_variable'];
                   }
               ?>
            </a>
        </li>   
mgunner
  • 1
  • 4
0

Maybe you want to try this instead, it worked for me.

<?php 
  if($_SESSION['username'] == true) { 
     echo '<a href="logout.php"><span>Logout</span></a>';
  }
  else {
    echo '<a href="login.php"><span>Login</span></a>';
  }
?>