我是编程新手,我正在使用html,mySQL和PHP来尝试为用户配置文件设置用户名和密码。然后,我想向用户显示自定义页面。
问题是,在我检查它们是否在数据库中或在数据库中之后,我无法弄清楚如何将它们直接发送到新页面。
我尝试使用我在谷歌上发现的一个名为header()
的功能,但我一直收到错误消息。我尝试通过确保在使用header()
函数之前没有输出到屏幕来解决它,我现在很确定我没有。我实际上并不知道问题是什么,我试图找到另一种方法来做到这一点,或者只是找到header()
的答案。无论如何,这是我的代码:
<?php
require "connect_to_mysql_and_test_connection.php";
//use the variable $db to access the database
?>
<html>
<head>
<title>User Login</title>
<h2>Login:</h2>
</head>
<body>
<!--this makes a form to take the user input. the input of username and password. the submit buttons value is always submitted to $_POST-->
<form method="post">
Create user name:<br>
<input type="text" name="username" /><br>
Create password:<br>
<input type="text" name="password"/><br>
<input type="submit" name="submit"/><br>
</form>
<?php
$compare_username="";
$compare_password="";
//get the username and password fields from the database and store it in a variable
$username_password_fields=$db->query("SELECT username, password FROM users");//mysql object
//get number of rows
$rows= $username_password_fields->num_rows;
//create a while loop to go through each row.
while($rows >=1){
//iterate through each array returned by fetch_assoc to see if the username already exists
foreach($username_password_fields->fetch_assoc() as $key=>$value){
//echo $key . $value . "<br>";
//************check to see if the username exists****************
if ($value === $_POST['username']){
$compare_username=$value;
}
else if ($value === $_POST['password']){
$compare_password=$value;
}
}
//check to see if the username matches the password
$rows -= 1;//decrement the rows remaining
if ($compare_username != "" and $compare_password != ""){
//write code to send them to their custom page
**header("Location:http://jasongriffore.atwebpages.com/myfirstworkingsite.php")<--This is the problem. please help!**
echo "Ding ding ding a match!";
return;
}
else if ($compare_username != ""){
echo "This user name exists but the password is incorrect";
//they can try again on the same page
return;
}
}
//if after running the while loop they didn't get sent to their custom page, the must be new!
echo "Let's create a new account for you!";
//enter code to add a new user to the database
echo print_r($_POST, true);
?>
答案 0 :(得分:1)
使用header()
功能:
header('Location: mypage.php');
请确保在您执行此操作之前没有输出发送到浏览器,因为我的HTML <form>
在您的PHP处理之前 之前是。改变它。
答案 1 :(得分:0)
使用javascript:
<script>window.location = 'http://jasongriffore.atwebpages.com/myfirstworkingsite.php';</script>
答案 2 :(得分:0)
我建议使用ob_start()
和ob_end_flush()
将输出缓冲到浏览器。您可以阅读有关此here的更多信息。您想要执行此操作的主要原因是,标头标记要求尚未向浏览器发送任何输出。通过回显<html><head>
...它已经将数据发送到客户端,因此不再可以发送标题(毕竟它们是标题,用于处理输出。)ob_start()
将缓冲(或存储) )所有内容,直到您准备输出它为止,并允许您稍后发送标题,因为输出尚未发送到浏览器。
<?php
require "connect_to_mysql_and_test_connection.php";
//use the variable $db to access the database
//Buffer the output
ob_start();
?>
<html>
<head>
<title>User Login</title>
<h2>Login:</h2>
</head>
<body>
<!--this makes a form to take the user input. the input of username and password. the submit buttons value is always submitted to $_POST-->
<form method="post">
Create user name:<br>
<input type="text" name="username" /><br>
Create password:<br>
<input type="text" name="password"/><br>
<input type="submit" name="submit"/><br>
</form>
<?php
$compare_username="";
$compare_password="";
//get the username and password fields from the database and store it in a variable
$username_password_fields=$db->query("SELECT username, password FROM users");//mysql object
//get number of rows
$rows= $username_password_fields->num_rows;
//create a while loop to go through each row.
while($rows >=1){
//iterate through each array returned by fetch_assoc to see if the username already exists
foreach($username_password_fields->fetch_assoc() as $key=>$value){
//echo $key . $value . "<br>";
//************check to see if the username exists****************
if ($value === $_POST['username']){
$compare_username=$value;
}
else if ($value === $_POST['password']){
$compare_password=$value;
}
}
//check to see if the username matches the password
$rows -= 1;//decrement the rows remaining
if ($compare_username != "" and $compare_password != ""){
//write code to send them to their custom page
header("Location:http://jasongriffore.atwebpages.com/myfirstworkingsite.php");
return;
}
else if ($compare_username != ""){
echo "This user name exists but the password is incorrect";
//they can try again on the same page
return;
}
}
//if after running the while loop they didn't get sent to their custom page, the must be new!
echo "Let's create a new account for you!";
//enter code to add a new user to the database
echo print_r($_POST, true);
//Write the output to the browser
ob_end_flush();
?>