duanpingzu7194 2014-03-12 01:39
浏览 84
已采纳

如何在php中使用admin和user登录表单

i want to make log-in form with admin and user, but i can't use the session_start() because it always make my variable undefined and it doesn't show any result. is there anybody who can help me? thanks in advance here is my code:

    <html>
    <body>

    <?php
    mysql_connect( "localhost" , "admin" , "123") or die(mysql_error());
    mysql_select_db("minimart_database") or die(mysql_error());
    ?>

    <form id="home_id" method ="POST" enctype ="multipart/form-data">
    <script>
    function submitForm(action)
    {
    document.getElementById('home_id').action=action;
    document.getElementById('home_id').submit();
    }
    </script>

    <p align = right> Username: <input type ="text" name ="user" placeholder="Enter username">
    <p align = right> Password: <input type ="password" name ="pass" placeholder="Enter password">
    <input type="submit" value="login"  name="submit" 

     <?php
    mysql_connect( "localhost" , "admin" , "123") or die(mysql_error());
    mysql_select_db("minimart_database") or die(mysql_error());
    ?>

    <?php 

    $user=$_POST['user'];
    $pass=$_POST['pass'];
    $user=stripslashes('$user');
    $pass=stripslashes('$pass');
    $user=mysql_real_escape_string('$user');
    $pass=mysql_real_escape_string('$pass');

    $query="SELECT * FROM account WHERE username='$user' AND password='$pass'";
    $result=mysql_query($query);
    $count=mysql_num_rows($result);
        $row=mysql_fetch_array($result);
    if (isset($_POST['submit']))

    if ($count==1){
    session_start();
    $session("user");
    $session("pass") ;


            if ($row['usertype']==0){
            header("location:admin_home.php");
            echo ("you logged in as admin");
    }
            elseif ($row['usertype']==1) {
            header("location:cashier_home.php");
            echo ("you logged in as cashier");
    }


    else {
    echo "invalid password and username";


    }
    }

    ?>
    </body>
    </html>

here is my updated code and the complete code for the whole form :

<html>
<body>

<?php
mysql_connect( "localhost" , "admin" , "123") or die(mysql_error());
mysql_select_db("minimart_database") or die(mysql_error());
?>

<form id="home_id" method ="POST" enctype ="multipart/form-data">
<script>
function submitForm(action)
{
document.getElementById('home_id').action=action;
document.getElementById('home_id').submit();
}
</script>

<p align = right> Username: <input type ="text" name ="user" placeholder="Enter username">
<p align = right> Password: <input type ="password" name ="pass" placeholder="Enter password">
<input type="submit" value="login"  name="submit" >


<?php 


if (isset($_POST['submit'])){
    $user=$_POST['user'];
$pass=$_POST['pass'];
$user=stripslashes('$user');
$pass=stripslashes('$pass');
$user=mysql_real_escape_string('$user');
$pass=mysql_real_escape_string('$pass');
$query="SELECT * FROM account WHERE username='$user' AND password='$pass'";
$result=mysql_query($query);
$count=mysql_num_rows($result);

if ($count==1){
session_start();
$_SESSION["user"]=$user;
$_SESSION["pass"]=$pass ;
$row=mysql_fetch_array($result);

        if ($row['usertype']==0){
        header("location:admin_home.php");
        echo ("you logged in as admin");
}
        elseif ($row['usertype']==1) {
        header("location:cashier_home.php");
        echo ("you logged in as cashier");
}


else {
echo "invalid password and username";


}
}
}
?>


<p align=left><input type="text" name="search" placeholder="enter barcode or item name"><input type="submit" name="searched" onclick="submitForm('finalhome.php')">

<?php
$output="";
if (isset($_POST['search'])){
$searchq=$_POST['search'];
$searchq=mysql_real_escape_string($searchq);
$order="SELECT * FROM stock WHERE barcode LIKE '%$searchq' OR itemname LIKE '%$searchq'";
$result=mysql_query($order);
$count=mysql_num_rows($result);

if ($count>=1){
        while($row=mysql_fetch_array($result)){
                $barcode=$row['barcode'];
                $itemname=$row['itemname'];
                $description=$row['description'];
                $amount=$row['amount'];
                $stocks=$row['stocks'];
                $location=$row['location'];
        }
            $output="<div>'.$barcode.' '.$itemname.' '.$description.' '.$amount.' '.$stocks.' '.$location.' </div>"; 
            }
            else
            {
            $output='no results';
            }
     }  
?>
<table border=5 align=center >
<tr><th>Barcode  </th><th>Item name </th><th>Description</th><th>Amount</th><th>Stock</th><th>Location</th>
</tr>
<tr><td>$_POST['$barcode']</td><td>$_POST['$itemname']</td><td>S_POST['$description']</td><td>$_POST['$amount']</td><td>$_POST['$stock']</td><td>$_POST['$location</td></tr>

</table>

<?php print("$output"); ?> 

</body>
</html>

the problem now, when the user log-in it doesn't go to admin_home.php or to the cashier_home.php. when i click log-in button, it shows the search data.

here is the code for my admin_home.php:

<?php
session_start();

if (isset[$_SESSION['user']=$user) || (isset[$_SESSION['pass']=$pass ){
echo "success login"; }


?>
  • 写回答

1条回答 默认 最新

  • duanbei1903 2014-03-12 02:13
    关注

    the problem seems to be in those lines:

    $session("user");
    $session("pass") ;
    

    in session_start() you will see that those lines are wrong, it should be

    $_SESSION["user"]=$user;
    $_SESSION["pass"]=$pass ;
    

    please, test this code (it shoul work, i hope ;) )

    <html>
    <body>
    <form id="home_id" method="POST" action="<?php echo $_SERVER[PHP_SELF]?>">
    <script>
    function submitForm(action)
    {
    document.getElementById('home_id').action=action;
    document.getElementById('home_id').submit();
    }
    </script>
    
    <p align = right> Username: <input type ="text" name ="user" placeholder="Enter username">
    <p align = right> Password: <input type ="password" name ="pass" placeholder="Enter password">
    <input type="submit" value="login"  name="submit" >
    
    </body>
    </html>
    
    <?php
    if (isset($_POST['submit'])){
    
            mysql_connect( "localhost" , "admin" , "123") or die(mysql_error());
            mysql_select_db("minimart_database") or die(mysql_error());
    
            $user=$_POST['user'];
            $pass=$_POST['pass'];
            $user=stripslashes('$user');
            $pass=stripslashes('$pass');
            $user=mysql_real_escape_string('$user');
            $pass=mysql_real_escape_string('$pass');
    
            $query="SELECT * FROM account WHERE username='$user' AND password='$pass'";
            $result=mysql_query($query) or die ("error: ".mysql_error());
    
                if (mysql_num_rows($result)==1){
    
                    session_start();
                    $_SESSION["user"]=$user;
                    $_SESSION["pass"]=$pass ;
    
                            $row=mysql_fetch_array($result);
                            if ($row['usertype']==0){
                            header("location:admin_home.php");
                            }
                            elseif ($row['usertype']==1) {
                            header("location:cashier_home.php");
                            }
    
                           }
                    else 
                    echo "invalid password and username";
    
    }//from isset(submit)
    ?>
    

    debugg..

    if (mysql_num_rows($result)==1){
            echo "only 1 record in database";
                    session_start();
                    $_SESSION["user"]=$user;
                    $_SESSION["pass"]=$pass ;
    
                            $row=mysql_fetch_array($result);
                            if ($row['usertype']==0){
                            echo "admin user";
                            }
                            elseif ($row['usertype']==1) {
                            echo "normal user";
                            }
    
        }
            else 
                    echo "invalid password and username";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000