我是php的新手(实际上根本不做网络开发),并且最近进入了它。我正在尝试创建一个登录页面,在登录后将用户重定向到链接到用户的唯一URL。所以我想在数据库中说4列:id,username,password和redirect。并且在成功登录后将URL重定向到存储用户URL的db中的重定向列。问题是我一直在处理的代码。要进行登录,我似乎无法使用标题(“location :)行重定向到db列。这是我的代码,也许你们可以帮助我:
<?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列变量来从
中的重定向列输出urlheader("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";
}
?>
答案 0 :(得分:2)
您需要将结果提取到关联数组中:
$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";
}