在PHP

时间:2015-09-01 12:48:56

标签: php mysql

当我打开viewrcd.php文件直接打开而不是打开admin_login.php页面时,我在使用$ _SESSION时遇到了麻烦。 (b'coz查看此页面“viewrcd.php”管理员必须登录...)

admin_login.php

<?php
session_start();
require 'testdbcheking.php';



    if(isset($_POST['login']))
    {

        $a_name = $_POST['admin_name1'];
        $a_pword = $_POST['admin_password1'];
        $_SESSION['admin_name1'] = 'admin_name1';
        if(!empty($_POST['admin_name1']) && !empty($_POST['admin_password1']))
            {
                $query10="SELECT * FROM admin_login WHERE admin_name = '$a_name' AND admin_password = '$a_pword'";
                if($query10_run = mysql_query($query10))
                {
                    $query10_num_rows = mysql_num_rows($query10_run);

                    if($query10_num_rows == 0)
                    {

                        echo 'you are not an admin';
                    }
                    if($query10_num_rows == 1)
                    {
                        echo "<script>window.open('viewrcd.php?logged= Admin logged in Success','_self')</script>";                 
                 }

                }
            }
        else
            {
                echo 'provide both......';
            }

    }

?>

<html>
    <title>Admin Login Page</title>
    <body>
        <form action = 'admin_login.php' method = 'POST' >
        <table align = 'center' border = '3' bgcolor = 'gray'> 
        <tr ><td colspan = 8 align ='center'>Admin Login FORM</td></tr>
        <tr>
            <td>Admin Username:</td>
            <td><input type = 'text' name ='admin_name1'></td>
        </tr>
        <tr>
            <td>Admin Password:</td>
            <td><input type = 'password' name ='admin_password1'></td>
        </tr>
        <tr>    <td colspan = '4' align ='center'>
                <input type = 'submit' name='login' value = 'Login'>
                </td>
        </tr>
        </table>
        </form>

    </body>

</html>

viewrcd.php

<?php
session_start();
require 'testdbcheking.php';



        if($_SESSION['admin_name1'])
        {
            //echo 'qqq';
            header('location :admin_login.php');
            //header('location : viewrcd.php');
        }
?>

<a href= 'admin_logout.php'>ADMIN_logout </a>
<html>
    <head><center><h1>Viewing all the rocords</center></h1><head/>

    <body>

        <table align = center border = 1>

        <tr align = 'center'>
            <th>SR.NO.</th>
            <th>Username</th>
            <th>Firstname</th>
            <th>Surname</th>
            <th>Delete</th>
            <th>Edit</th>
            <th>Details</th>

        </tr>
            <?php 
                $query4 = 'SELECT * FROM users';
                $query4_run = mysql_query($query4);
                while (@$data_row = mysql_fetch_array($query4_run))
                {
                    $u_id = $data_row [0];
                    $u_name = $data_row[1];
                    $f_name = $data_row [3];
                    $s_name = $data_row [4];

            ?>
        <tr align = 'center'>   
            <td> <?php echo @$u_id;?></td>
            <td> <?php echo @$u_name;?></td>
            <td> <?php echo @$f_name;?></td>
            <td> <?php echo @$s_name;?></td>
            <td><a href = 'delete.php?del=<?php echo $u_id ;?>'>Delete</a></td>
            <td><a href = 'edit.php?edit=<?php echo $u_id ;?>'>Edit</a></td>
            <td><a href = 'viewrcd.php?details=<?php echo $u_id ;?>'>Details</a></td>
            <?php } ?>
        </tr>
        </table>
        <?php
                $record_details = @$_GET['details'];
                $query8 = "SELECT * FROM users WHERE id = '$record_details'";
                $query_run8 = mysql_query($query8);
                while($query_run8_row = mysql_fetch_array($query_run8))
                {
                    $d_name = $query_run8_row[1];
                    $d_firstname = $query_run8_row[3];
                    $d_surname = $query_run8_row[4];
        ?>

        <table align = center border = 1>
            </tr>
            <tr ><td colspan = 10 align = center> your details are here </td></tr>
            <tr align = center>
            <td><?php echo $d_name ; ?></td>
            <td><?php echo $d_firstname ; ?></td>
            <td><?php echo $d_surname ; ?></td>
            </tr>
        <?php } ?>
        </table>
        <font bgcolor = 'green' align = 'center' size = '6'>
        <?php echo @$_GET['deleted'];?>
        </font>
        <font color = 'green' align = 'center' size = '6'>
        <?php echo @$_GET['updated'];?>
        <?php echo @$_GET['logged'];?>
        </font><br><br><br><br><br>
        <form action = "viewrcd.php" method = "get">
        Search record: <input type = "text" name = "search" >
        <input type = "submit" name = "submit" value ="Find Record">        
        </form>
        <?php
        if (isset($_GET['search']))
        {
            $search_record = $_GET['search'];
            $query9 = "SELECT * FROM users WHERE id = '$search_record' OR firstname ='$search_record' ";
            $query9_run = mysql_query($query9);
            while($query9_run_row = mysql_fetch_assoc($query9_run))
            {
                //$find_id = $query9_run_row['id'];
                $find_username = $query9_run_row['username'];
                $find_firstname = $query9_run_row['firstname'];
                $find_surname = $query9_run_row['surname'];
            ?>
            <br><br><br>
            <table border = '2' bgcolor ='green' align ='center'>
                <tr>
                <th>Username</th>
                <th>Firstname</th>
                <th>Surname</th>
                <tr>
                <td><?php echo $find_username ;  ?></td>
                <td><?php echo $find_firstname ;  ?></td>
                <td><?php echo $find_surname ;  ?></td>
                </tr>
            </table>
<?php   }   }   ?>
    </body>
</html>

admin_logout.php

<?php

require 'testdbcheking.php';
session_start();
session_destroy();
header('Location: admin_login.php');

?>

3 个答案:

答案 0 :(得分:0)

替换

if($_SESSION['admin_name1'])

if(!$_SESSION['admin_name1'])

在viewrcd.php中

答案 1 :(得分:0)

替换

if($_SESSION['admin_name1'])

if(empty($_SESSION['admin_name1']))

答案 2 :(得分:0)

我已经更新了我的答案..现在

<强> admin_login.php

       <?php
session_start();
require 'testdbcheking.php';

global $a_name;

if(isset($_POST['login']))
{

    $a_name = $_POST['admin_name1'];
    $a_pword = $_POST['admin_password1'];

    if(!empty($_POST['admin_name1']) && !empty($_POST['admin_password1']))
    {
        $query10="SELECT * FROM admin_login WHERE admin_name = '$a_name' AND admin_password = '$a_pword'";
        if($query10_run = mysql_query($query10))
        {
            $query10_num_rows = mysql_num_rows($query10_run);

            if($query10_num_rows == 0)
            {

                echo 'you are not AN admin';
            }
            if($query10_num_rows == 1)
            {
                $admin_id = mysql_result($query10_run,0,'admin_name');
                $_SESSION['admin_id'] = $admin_id;

                echo "<script>window.open('viewrcd.php?logged= Admin logged in Success','_self')</script>";                 
            }

        }
    }
    else
    {
        echo 'provide both......';
    }

}

?>
<br><br><br>
<?php //echo $a_name ; ?>
<html>
<title>Admin Login Page</title>
<body>
    <form action = 'admin_login.php' method = 'POST' >
        <table align = 'center' border = '3' bgcolor = 'gray'> 
            <tr ><td colspan = 8 align ='center'>Admin Login FORM</td></tr>
            <tr>
                <td>Admin Username:</td>
                <td><input type = 'text' name ='admin_name1'></td>
            </tr>
            <tr>
                <td>Admin Password:</td>
                <td><input type = 'password' name ='admin_password1'></td>
            </tr>
            <tr>    <td colspan = '4' align ='center'>
                <input type = 'submit' name='login' value = 'Login'>
            </td>
        </tr>
    </table>
</form>

</body>

</html>

<强> viewrcd.php

    <?php
session_start();
require 'testdbcheking.php';

if(!isset($_SESSION['admin_id']))
{
    ?>
    <a href="admin_login.php">First Login here..</a>
    <?php 
}
else

{
    ?>
    <a href= 'admin_logout.php'>ADMIN_logout </a>
    <html>
    <head><center><h1>Viewing all the rocords</center></h1><head/>

        <body>

            <table align = center border = 1>

                <tr align = 'center'>
                    <th>SR.NO.</th>
                    <th>Username</th>
                    <th>Firstname</th>
                    <th>Surname</th>
                    <th>Delete</th>
                    <th>Edit</th>
                    <th>Details</th>

                </tr>
                <?php 
                $query4 = 'SELECT * FROM users';
                $query4_run = mysql_query($query4);
                while (@$data_row = mysql_fetch_array($query4_run))
                {
                    $u_id = $data_row [0];
                    $u_name = $data_row[1];
                    $f_name = $data_row [3];
                    $s_name = $data_row [4];

                    ?>
                    <tr align = 'center'>   
                        <td> <?php echo @$u_id;?></td>
                        <td> <?php echo @$u_name;?></td>
                        <td> <?php echo @$f_name;?></td>
                        <td> <?php echo @$s_name;?></td>
                        <td><a href = 'delete.php?del=<?php echo $u_id ;?>'>Delete</a></td>
                        <td><a href = 'edit.php?edit=<?php echo $u_id ;?>'>Edit</a></td>
                        <td><a href = 'viewrcd.php?details=<?php echo $u_id ;?>'>Details</a></td>
                        <?php } ?>
                    </tr>
                </table>
                <?php
                $record_details = @$_GET['details'];
                $query8 = "SELECT * FROM users WHERE id = '$record_details'";
                $query_run8 = mysql_query($query8);
                while($query_run8_row = mysql_fetch_array($query_run8))
                {
                    $d_name = $query_run8_row[1];
                    $d_firstname = $query_run8_row[3];
                    $d_surname = $query_run8_row[4];
                    ?>

                    <table align = center border = 1>
                    </tr>
                    <tr ><td colspan = 10 align = center> your details are here </td></tr>
                    <tr align = center>
                        <td><?php echo $d_name ; ?></td>
                        <td><?php echo $d_firstname ; ?></td>
                        <td><?php echo $d_surname ; ?></td>
                    </tr>
                    <?php } ?>
                </table>
                <font bgcolor = 'green' align = 'center' size = '6'>
                    <?php echo @$_GET['deleted'];?>
                </font>
                <font color = 'green' align = 'center' size = '6'>
                    <?php echo @$_GET['updated'];?>
                    <?php echo @$_GET['logged'];?>
                </font><br><br><br><br><br>
                <form action = "viewrcd.php" method = "get">
                    Search record: <input type = "text" name = "search" >
                    <input type = "submit" name = "submit" value ="Find Record">        
                </form>
                <?php
                if (isset($_GET['search']))
                {
                    $search_record = $_GET['search'];
                    $query9 = "SELECT * FROM users WHERE id = '$search_record' OR firstname ='$search_record' ";
                    $query9_run = mysql_query($query9);
                    while($query9_run_row = mysql_fetch_assoc($query9_run))
                    {
                //$find_id = $query9_run_row['id'];
                        $find_username = $query9_run_row['username'];
                        $find_firstname = $query9_run_row['firstname'];
                        $find_surname = $query9_run_row['surname'];
                        ?>
                        <br><br><br>
                        <table border = '2' bgcolor ='green' align ='center'>
                            <tr>
                                <th>Username</th>
                                <th>Firstname</th>
                                <th>Surname</th>
                                <tr>
                                    <td><?php echo $find_username ;  ?></td>
                                    <td><?php echo $find_firstname ;  ?></td>
                                    <td><?php echo $find_surname ;  ?></td>
                                </tr>
                            </table>
                            <?php   }   }   ?>
                        </body>
                        </html>
                        <?php }?>

管理-logout.php

  <?php
session_start();
// session_destroy();
unset($_SESSION['admin_id']);
header('Location: loginform.php');

?>