Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我可以从连接到登录的mysql数据库中提取URL吗?_Php_Sql_Database_Redirect_Login - Fatal编程技术网

Php 我可以从连接到登录的mysql数据库中提取URL吗?

Php 我可以从连接到登录的mysql数据库中提取URL吗?,php,sql,database,redirect,login,Php,Sql,Database,Redirect,Login,我想知道我是否可以在数据库中使用一个URL连接到一个登录名,当用户登录脚本时,该URL将读取重定向到该登录名的URL?或者关于简单的唯一登录重定向的其他想法 我的数据库由ID/username/password/url列组成-显然,我对连接内容进行了排序,尽管您不需要查看它们;) 我的代码是 mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")o

我想知道我是否可以在数据库中使用一个URL连接到一个登录名,当用户登录脚本时,该URL将读取重定向到该登录名的URL?或者关于简单的唯一登录重定向的其他想法

我的数据库由ID/username/password/url列组成-显然,我对连接内容进行了排序,尽管您不需要查看它们;)

我的代码是

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

$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);

$count=mysql_num_rows($result);
$count=mysql_num_rows($result);

if($count==1){

session_register("myusername");
session_register("mypassword"); 
header('Location: '.$row['url']);
}
else {
echo "Wrong Username or Password";
}

ob_end_flush();
?>

在数据库中的登录字段中添加一个指定url的字段。现在,当用户登录时,检索URL,然后使用以下代码重定向到检索到的URL

header('Location: http://www.example.com/');

是的,你可以。在登录时,只需从数据库表中读取URL并使用重定向,您不会在您的数据库中创建这样的内容

您可以在数据库中放置一个url字段,以便在他们登录时使用

在PHP脚本中,通过从DB获取结果行(当然,更复杂的登录会做得更多),验证用户是否确实存在,以及他/她的密码是否正确后,您可以使用以下方法:

header('Location: '.$row['url']);
根据用户的登录状态将其重定向到另一个页面。尽管作为提示,请确保使用某种$\u会话变量记录它们,该变量是在重定向它们之前设置的,并在另一个页面上检查其值

编辑 不推荐使用会话\u寄存器:


去掉那个end-PHP标记,你真的不想要它。我认为这应该行得通,您可能会遇到一个SQL错误,在这种情况下,您可能会发现是引号引起了麻烦,应该用
封装字段值,但我总是忘记mysql\u real\u escape\u字符串是如何转义的,所以Ima现在就这样保留它。

另外请注意:在发送您的重定向标题。如果我有一个id为/username/password/url$myusername=stripslashes($myusername)的数据库,我该如何写这个标题$mypassword=stripslashes($mypassword)$myusername=mysql\u real\u escape\u字符串($myusername)$mypassword=mysql\u real\u escape\u字符串($mypassword)$sql=“从$tbl\U名称中选择*,其中用户名='$myusername'和密码='$mypassword'$result=mysql\u查询($sql)$count=mysql\u num\u行($result)$count=mysql\u num\u行($result);如果($count==1){session_register(“myusername”);session_register(“mypassword”);header('Location:'.$row['url']);}否则{echo“错误的用户名或密码”}@user1493388您能用此代码编辑原始问题吗?然后我们可以告诉您最好放在哪里。@user1493388编辑了我的答案,请查看
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

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

$count=mysql_num_rows($result);

if($count==1){

    $_SESSION['myusername'] = $myusername;
    $_SESSION['mypassword'] = $mypassword;

    while($row = mysql_fetch_assoc($result)){
        header('Location: '.$row['url']);
        exit(); // As a user above mentions this is actually quite important
    }

}else{
    echo "Wrong Username or Password";
}