gpt4 book ai didi

PHP:使用 header 从数据库打开网址

转载 作者:行者123 更新时间:2023-11-29 08:57:11 25 4
gpt4 key购买 nike

我对 php 还很陌生(实际上大多数情况下根本不进行 Web 开发),并且最近才开始接触它。我正在尝试创建一个登录页面,在登录后将用户重定向到链接到该用户的唯一 URL。所以我想在数据库中有 4 列:id、用户名、密码和重定向。成功登录后,将 URL 重定向到存储用户 URL 的数据库中的重定向列。问题是我一直在处理的代码。要进行登录,我似乎无法使用 header("location:) 行将其重定向到数据库列。这是我的代码,也许你们可以帮助我:

<?php
$host="sql1om"; // Host name
$username="b33_1033"; // Mysql username
$password="b33_1033"; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

所以我不知道如何添加 db 列变量以从重定向列输出 url

header("location:login_success.php);

每次我尝试该行中的变量时,都不会发生任何情况任何帮助,将不胜感激。请彻底解释一下我是php新手。谢谢!

这是我编辑后的代码(在帮助下):

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file"login_success.php"
$_SESSION['username'] = $myusername;
$_SESSION['password'] = $mypassword;
$result = mysql_query("SELECT redirect FROM members");

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
header("Location: " . $row['redirect']);
}
exit();
}
else {
echo "Wrong Username or Password";
}
?>

最佳答案

您需要将结果提取到关联数组中:

$row = mysql_fetch_assoc($result)

然后只需使用 header("Location: $row['redirect']");

  • 将字符串括在双引号中使您可以在其中插入变量。

  • 或者,您也可以连接:header("Location: ". $row['redirect']);

  • 您的 SQL 结果将存储在名为 $row 的关联数组中。要访问redirect 列,请使用$row['redirect']。当然,仅当找到一行或多行时才会填充$row

  • 最后,如果您正在重定向,最好使用 exit() 来立即停止脚本执行。

您的代码如下所示:

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; 
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['username'] = $myusername;
$_SESSION['password'] = $mypassword;
$row = mysql_fetch_assoc($result);
header("Location: $row['redirect']");
exit();
}else {
echo "Wrong Username or Password";
}

关于PHP:使用 header 从数据库打开网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9659584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com