首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过以下教程创建一个小型论坛

问通过以下教程创建一个小型论坛
EN

Stack Overflow用户
提问于 2019-11-20 11:17:05
回答 1查看 70关注 0票数 0

如果有人能把我引向正确的方向,那将是我的一天!

我试图通过以下教程创建一个论坛:"https://code.tutsplus.com/tutorials/how-to-create-a-phpmysql-powered-forum-from-scratch--net-10188“。

我在创建页面时做了一些修改,但我遇到的问题是登录时,首先,当我将connect.php页面添加到登录页时,代码不会回显表单,而是空白。另外,当我不使用connect页面时,错误信息会在开始时打印出来,当我希望它们在点击submit之后出现时。

我已经设法连接到我的数据库,用其他代码获取数据,但我似乎无法做到这一点。

代码语言:javascript
复制
<?php
session_start();

//signin.php
include 'forumHeader.php';
include 'connect.php';
echo '<h3>Sign in</h3>';



if(isset($_SESSION['signed_in']) && $_SESSION['signed_in'] == true)
{
    echo 'You are already signed in, you can <a href="forumSignout.php">sign out</a> if you want.</br></br>';
    echo 'Welcome, ' . $_SESSION['user_name'] . '. <a href="forumIndex.php">Proceed to the forum overview</a>.';

}

else
{




  if($_SERVER['REQUEST_METHOD'] != 'POST')
  {
      /*the form hasn't been posted yet, display it
        note that the action="" will cause the form to post to the same page it is on */
      echo '<form method="post" action="">
          Username: <input type="text" name="user_name" />
          Password: <input type="password" name="user_pass"/>
          <input type="submit" value="Sign in" />
       </form>';
  }
    /* so, the form has been posted, we'll process the data in three steps:
        1.  Check the data
        2.  Let the user refill the wrong fields (if necessary)
        3.  Varify if the data is correct and return the correct response
    */
    $errors = array(); /* declare the array for later use */


    if(!isset($_POST['user_name'])) //NOT + FALSE + POST FROM INPUT  //ISSET RETURNS FALSE WHEN CHECKING THAT HAS BEEN ASSIGNED TO NULL
    {
        $errors[] = 'The username field must not be empty.';
    }

    if(!isset($_POST['user_pass']))
    {
        $errors[] = 'The password field must not be empty.';
    }

    if(!empty($errors)) /*check for an empty array, if there are errors, they're in this array (note the ! operator)*/ //Detta betyder, om ERRORS INTE är TOM
    {
        echo 'Uh-oh.. a couple of fields are not filled in correctly..';
        echo '<ul>';
        foreach($errors as $key => $value) /* walk through the array so all the errors get displayed */
        {
            echo '<li>' . $value . '</li>'; /* this generates a nice error list */
        }
        echo '</ul>';


    }



    else
    {
        //the form has been posted without errors, so save it
        //notice the use of mysql_real_escape_string, keep everything safe!
        //also notice the sha1 function which hashes the password
        $sql = "SELECT
                    user_id,
                    user_name,
                    user_level
                FROM
                    forum_Users
                WHERE
                    user_name = '" . mysql_real_escape_string($_POST['user_name']) . "'
                AND
                    user_pass = '" . sha1($_POST['user_pass']) . "'";

        $result = mysql_query($sql);
        if(!$result)
        {
            //something went wrong, display the error
            echo 'Something went wrong while signing in. Please try again later.';
            //echo mysql_error(); //debugging purposes, uncomment when needed
        }
        else
        {
            //the query was successfully executed, there are 2 possibilities
            //1. the query returned data, the user can be signed in
            //2. the query returned an empty result set, the credentials were wrong
            if(mysql_num_rows($result) == 0)
            {
                echo 'You have supplied a wrong user/password combination. Please try again.';
            }
            else
            {
                //set the $_SESSION['signed_in'] variable to TRUE
                $_SESSION['signed_in'] = true;

                //we also put the user_id and user_name values in the $_SESSION, so we can use it at various pages
                while($row = mysql_fetch_assoc($result))
                {
                    $_SESSION['user_id']    = $row['user_id'];
                    $_SESSION['user_name']  = $row['user_name'];
                    $_SESSION['user_level'] = $row['user_level'];
                }

                echo 'Welcome, ' . $_SESSION['user_name'] . '. <a href="forumIndex.php">Proceed to the forum overview</a>.';
            }
        }
    }
}


include 'forumFooter.php';
?>

这就是我在登录页面中使用的代码。我在connect.php页面上的代码是:

代码语言:javascript
复制
<?php
//connect.php


$server = 'server';
$username   = 'user';
$password   = 'pass';
$database   = 'database';

if(!mysql_connect($server, $username, $password))
{
    exit('Error: could not establish database connection');
}
if(!mysql_select_db($database)
{
    exit('Error: could not select the database');
}

?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-20 11:27:58

在echo中,如果存在$_POST,则应该将else放到正在处理的表单中,无论是否存在$_POST,您都要去那里,并且尝试处理空的$_POST会抛出错误。

附带注意:将错误报告设置为使用此方法error_reporting(E_ALL)的所有人,这将使您知道将来发生了什么错误,通常设置在设置session_start()的位置。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58953087

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档