0

我有一点问题,请相信你们中的一位专家可以提供帮助:-p。

我在数据库中有以下表:

b_admins:

id
username
password
email
locationid

b_locations

id
name
description

登录脚本:

<?php

    // Inialize session
    session_start();

    // Connection to MySQL Database.
    include ('_includes/_dbconnection.php');
    include ('_includes/_dbopen.php');

    // Retrieve username and password from database according to user's input
    $login = mysql_query("SELECT * FROM b_admins WHERE (ausername = '" . mysql_real_escape_string($_POST['username']) . "') and (apassword = '" .           mysql_real_escape_string($_POST['password']) . "')");

    // Check username and password match
    if (mysql_num_rows($login) == 1) {

    // Set username session variable
    $_SESSION['username'] = $_POST['username'];

    // Set locationid session varaible //// Modified 12 April 2012.
    $_SESSION['locationid'] = $row_Recordset1["locationid"];

    $locationid = $_SESSION['locationid'];

    // Jump to secured page
    header('Location: _home.php');
    }

    else {

    // Jump to login page
    header('Location: index.php');
    }

    // Script Source: http://frozenade.wordpress.com/2007/11/24/how-to-create-login-page-in-php-and-mysql-with-session/

?>

我的主页脚本:

    // Check, if user is already login, if login proceed to _home.php
    if (isset($_SESSION['username'])) {
    header('Location: _home.php');
    }


    // Load Header file.
    include ('_includes/_header.php');

    // Template Construction.

    // Create Homepage Layout after $home01.
    $home01 = '<div id="imglogo"></div>
                <div id="container">
                <div class="top"> 
                <div id="logo">
                </div>
                <ul class="socialicons">
                </ul>
                </div>
                <div id="content" >
                <div id="home"> 
                <div class="latest">';


    // MySQL Load Location Name from MySQL database after $home02.
    $home02 = '<h3>';


    // Create Notification Area for the Administrator after $home03.
    $home03 = '&nbsp; Administrator Panel</h3><p>You are logged in to make changes to your Branch, only your Branch details can be changed  within this Administration Panel. You may proceed to make any changes using the menu at the bottom.</p></div>
                <!-- /Latest section -->

                <!-- News info section -->
                <ul class="news-info">
                <li><label>Users Registered</label>';


    //MySQL Load User count from MySQL database after $home04.
    $home04 = '<span>';


    // Close Template.
    $home05 = '</span></li>
                </ul>
                <!-- /News info section -->
                </div>        
                <!-- /Home --> 

                <!-- Menu -->
                <div class="menu">
                <ul class="tabs">
                <li><a href="_home.php" class="tab-home"></a></li>
                <li><a href="_events.php" class="tab-events"></a></li>
                <li><a href="_gallery.php" class="tab-portfolio"></a></li>
                <li><a href="_template_edit.php" class="tab-contact"></a></li>
                </ul>
                </div>
                </div>';

    ////////////////////////////////////////////////////////////////////////// Combining Template with MySQL Data.

    /// Build Home Page from MySQL database.

     $SQL = "SELECT * FROM b_admins, b_locations WHERE locationid ='$locationid'  LIMIT 0, 1";
            $result = mysql_query($SQL);

            while ($db_field = mysql_fetch_assoc($result)) {

                echo $home01;
                echo $home02;
                print $db_field['lcity'];
                echo $home03;
            }

    /// Build Home Page News Section from MySQL database.
                echo $home04;
    $SQL = "SELECT count( * ) as total_record  FROM `b_users` WHERE locationid = '$locationid'";
                echo $home05;

    /// Load Footer and Database close file.
    include ('_includes/_footer.php');
    include ('_includes/_dbclose.php');

?>

我想从 b_admins 中保存值“locationsid”,以便能够使用 SQL 从数据库中检索数据,其中“locationsid”与数据库 b_locations 中的表相同。

我还收到以下错误,我不知道如何通过登录脚本登录时修复:“ http://localhost/xxxxx/_home.php上的网页导致重定向过多。”

提前致谢 :-)

4

3 回答 3

1

好像你需要先获取记录你需要获取 Mysql 结果才能获取记录

$row_Recordset1 = mysql_fetch_array($login, MYSQL_ASSOC);
$_SESSION['locationid'] = $row_Recordset1["locationid"];

主页也需要一个 session_start(); 或者它永远不会有效,因为仅在函数调用后加载页面时不存在会话

如果您仍然以标题重定向结束,您可以使用 html 元刷新还注意您不要将重定向指向同一页面

希望能帮助到你

于 2012-04-12T13:48:33.870 回答
0

当你说“主页脚本”时,这个文件是 index.php 还是 _home.php?

如果是_home.php,则不需要以下几行:

// Check, if user is already login, if login proceed to _home.php
if (isset($_SESSION['username'])) {
header('Location: _home.php');
}

这可能是导致重定向循环的原因。

于 2012-04-12T12:34:08.947 回答
0

我认为您的查询也是错误的。

// Retrieve username and password from database according to user's input
$login = mysql_query("SELECT * FROM b_admins WHERE (ausername = '" .mysql_real_escape_string($_POST['username']) . "') and (apassword = '" .          mysql_real_escape_string($_POST['password']) . "')");

你的代码有错别字。

ausername应该是username * apassword * 应该是passsword

于 2012-04-12T12:44:48.940 回答