首页 > 解决方案 > 尝试按照教程创建一个小型论坛

问题描述

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

我正在尝试按照本教程创建论坛:“ https://code.tutsplus.com/tutorials/how-to-create-a-phpmysql-powered-forum-from-scratch--net-10188 ”。

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

我已经设法连接到我的数据库并使用其他代码获取数据,但我似乎无法让它工作。

<?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 页面上的代码是:

<?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');
}

?>

标签: php

解决方案


如果您正在echo输入表单,则应该else输入正在处理的表单(如果有)$_POST,无论是否存在,atm 您都将进入它,$_POST并且尝试处理 empty $_POSTs 会引发错误。

旁注:使用此方法将您的错误报告设置为所有error_reporting(E_ALL),这将使您知道将来出了什么问题,通常设置在您设置的位置session_start()


推荐阅读