driuwt9557 2013-09-06 09:46
浏览 152
已采纳

在右上角的标题内容中登录表单

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

  • 写回答

1条回答 默认 最新

  • douxiao0400 2013-09-06 10:14
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面