Php 尝试按照教程创建一个小型论坛
如果有人能给我指出正确的方向,那将是我的一天 我正试图通过以下教程创建一个论坛:“” 我已经创建了一些修改过的页面,但我遇到的问题是在登录时,首先,当我将connect.php页面添加到登录页面时,代码没有响应表单,它是空白的。另外,当我不使用连接页面时,当我希望在点击提交后出现错误消息时,错误消息会在开始时打印出来 我已经设法与我的数据库建立了连接,并用其他代码获取了数据,但我似乎无法让它正常工作Php 尝试按照教程创建一个小型论坛,php,Php,如果有人能给我指出正确的方向,那将是我的一天 我正试图通过以下教程创建一个论坛:“” 我已经创建了一些修改过的页面,但我遇到的问题是在登录时,首先,当我将connect.php页面添加到登录页面时,代码没有响应表单,它是空白的。另外,当我不使用连接页面时,当我希望在点击提交后出现错误消息时,错误消息会在开始时打印出来 我已经设法与我的数据库建立了连接,并用其他代码获取了数据,但我似乎无法让它正常工作 <?php session_start(); //signin.php include
<?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';
?>
你在哪里回送出你应该处理的表单否则
进入正在处理的表单如果有$\u POST
,无论是否有$\u POST
,你都要去atm,并且尝试处理空$\u POST
将抛出错误
旁注:使用此方法将您的错误报告设置为allerror\u reporting(E\u all)
,这将让您知道将来会出现什么问题,通常在设置session\u start()
的位置设置,我建议使用CakePHP之类的框架或Slim之类的较小框架。使用composer作为依赖项,您就不必担心从头开始创建东西,谢谢您!!没问题,请注意,一旦项目发布并向公众开放,您应该将其关闭。。不想给坏演员比你需要的更多的信息。
<?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');
}
?>