0

i made a log in form in header on the top right side with the other contents like search bar and logo

and i have a plan when i logged in the other headers content is stand still but only the log in content is changing to the Logged in content

and here is my code that i assume have no problem at all

PHP code

if(empty($_POST) === false){
$username = $_POST['input-username'];
$password = $_POST['input-password'];

    if(empty($username) === true || empty($password) ===true){
        $errors[] = 'You Need To Enter a Username And a Password';
        }
        else if(user_exists($username) === false){
        $errors[] = 'We Can\'t Find That Username';
            }

        else{
            $login = login($username, $password);

                if($login === false){
                    $errors[] = 'Your Username and Password Combination is Incorrect';
                    }
                else{
                    $_SESSION['user_id'] = $login;
                    header("Location: index.php");
                    }
            }
        print_r($errors);
}

HTML Script

  <form method="post" action="index.php" name="signin-form">
      <table border="1">

                <tr>
                    <td>Username</td><td><input type="text" name="input-username" id="input-username"></td>
                    <td>Password</td><td><input type="password" name="input-password" id="input-password"></td>

                    <td>
                    <input type="submit" name="sign-in" id="sign-in" value="Log in">
                    </td>
                </tr>

                <tr>
                     <td colspan="2">
                     <input type="checkbox" name="remember-me">
                     <label id="information">Keep Me Eating Picture</label>
                     </td>
                     <td colspan="2">
                     <a href="#" id="forgotpass">Forgotten My Username or Password</a>
                     </td>
                 </tr>
     </table>
 </form>

but somehow the condition is getting weird after i input the username and password why that i should click the submit button twice in the log in content to redirect to logged in content?

like this: username obink password obink and enter

first log in is nothing happened but the username field and password field are empty the second log in with the blank fields of username and password is the time to redirect the log in content to the logged in content

is it my code is going wrong or what? i had trying to rebuild the css but there is nothing happened. And ow ya, i have users function page already, it is the page which is storing any function what i need

here is the headers content design more or less:

div logo

div search box

and div user log in or not like this

        if (logged_in($user_data['user_id']) === false){
        include 'login.php';
        }
    else{
        include 'logged_in.php';
        }

all is float left

and the php in the user functions

function logged_in(){
return (isset($_SESSION['user_id'])) ? true : false;
    }

in my source.php page

    if(logged_in() === true)
{$sid = $_SESSION['user_id'];
$user_data = user_data($_SESSION['user_id'], 'user_id', 'username', 'password', 'firstname', 'lastname', 'email');
}

renew the condition on my headers html on headers

    <div id="header">
    <div id="header-content">
<div id="logo">
<a href="index.php" style="text-decoration:none;">
          logo</a>
</div>

<div id="searchbox">
      <input type="text" placeholder="Search" size="50">
</div>

php on the middle of headers

if(empty($_SESSION['user_id'])){
include 'includes/widgets/login.php';
}

else{
    include 'includes/widgets/loggedin.php';
    }

closing div

</div>
</div>

sorry for my English

Obink
  • 229
  • 2
  • 13

1 Answers1

0

Try this code:

<?php
session_start();
if(empty($_SESSION['user_id'])){
?>
<form method="post" action="index.php" name="signin-form">
      <table border="1">

                <tr>
                    <td>Username</td><td><input type="text" name="input-username" id="input-username"></td>
                    <td>Password</td><td><input type="password" name="input-password" id="input-password"></td>

                    <td>
                    <input type="submit" name="sign-in" id="sign-in" value="Log in">
                    </td>
                </tr>

                <tr>
                     <td colspan="2">
                     <input type="checkbox" name="remember-me">
                     <label id="information">Keep Me Eating Picture</label>
                     </td>
                     <td colspan="2">
                     <a href="#" id="forgotpass">Forgotten My Username or Password</a>
                     </td>
                 </tr>
     </table>
 </form>
<?php
} else {
 echo "Logged in! <a href='logout.php'>Logout</a>";
 //You can display the users name here; Example: echo "Hi, ".$user_data['name'];
}
?>

PHP script:

<?php
if(empty($_POST) === false){
$username = $_POST['input-username'];
$password = $_POST['input-password'];

    if(empty($username) === true || empty($password) ===true){
        $errors[] = 'You Need To Enter a Username And a Password';
        }
        else if(user_exists($username) === false){
        $errors[] = 'We Can\'t Find That Username';
            }

        else{
            $login = login($username, $password);

                if($login === false){
                    $errors[] = 'Your Username and Password Combination is Incorrect';
                    }
                else{
                    $_SESSION['user_id'] = $login;
                    header("Location: index.php");
                    }
            }
        print_r($errors);
}
?>

This will solve your login problem

Suresh Peiris
  • 396
  • 2
  • 14
  • what is the different? ow ya i stored my session on the different page so i just including it – Obink Sep 06 '13 at 10:34
  • i have this one in the users function function logged_in(){ return (isset($_SESSION['user_id'])) ? true : false; } and after that in the headers div logo div searchbox and div php filled by choice if (logged_in($user_data['user_id']) === false){ include 'login.php'; } else{ include 'logged_in.php'; } – Obink Sep 06 '13 at 10:42
  • I made an edit of the code. I added extra PHP code... Now you don't need to login twice – Suresh Peiris Sep 06 '13 at 11:10
  • aye i have tried that way but the only shows up is this things Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\bacon\new_web\all_links.php:30) in C:\xampp\htdocs\bacon\new_web\includes\widgets\login.php on line 21 Array ( ) – Obink Sep 06 '13 at 12:40
  • and after that shows up i should log in again without fill anything on the username and password fields.. – Obink Sep 06 '13 at 12:41
  • This forum will helps you: http://stackoverflow.com/questions/1793482/php-error-cannot-modify-header-information-headers-already-sent – Suresh Peiris Sep 06 '13 at 13:07
  • WHAT a gun!!!! i just wrong wrote ob_start(); what i wrote was an ob_flush(); thx buddy :D – Obink Sep 06 '13 at 13:29