Pergunta

i am using a script at front end, if a user logs in, it should render username in place of login button else it should remain the same. but my script is not not working....

<?php 
    if(!isset($_SESSION['txt_username']) || !isset($_SESSION['txt_pwd'])){
    echo "<a href='login.php'>Log in</a>"; 
    header("Location:login.php");
    else {echo $_SESSION['txt_username']}
    }
?>
Foi útil?

Solução

There are a couple of syntax errors. Properly formatting your code will help dramatically when trying to diagnose errors:

<?php 
if(!isset($_SESSION['txt_username']) || !isset($_SESSION['txt_pwd'])){
    //echo "<a href='login.php'>Log in</a>";                                          
    header("Location:login.php");
}else{
    echo $_SESSION['txt_username'];
}

Outras dicas

The header function of php does the redirect immediatly when it finds that line. (it does other stuff also :) ) Read at: http://php.net/manual/en/function.header.php .

try this:

if(!isset($_SESSION['txt_username']) || !isset($_SESSION['txt_pwd'])){
    echo "<a href='login.php'>Log in</a>";                                          
else {
    echo $_SESSION['txt_username'];
}

In the ending: DO NOT KEEP THE PASSWORD IN $_SESSION!!!! . At least use md5() or some encryption on that password.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top