PHP会话。将输入值从一个页面传递到另一个页面
我试图使用php会话将输入值从一个页面传递到另一个页面。整个晚上都在试图解决这个问题,但没有结果。希望你能帮助我。以下是我的php脚本:PHP会话。将输入值从一个页面传递到另一个页面,php,session,Php,Session,我试图使用php会话将输入值从一个页面传递到另一个页面。整个晚上都在试图解决这个问题,但没有结果。希望你能帮助我。以下是我的php脚本: <?php $dbc = mysqli_connect ('localhost', 'root', 'root', 'db') or die ('Error connecting'); $lt = $_POST ['lt']; $lg = $_POST ['lg']; $loc= $_POST ['loc'];
<?php
$dbc = mysqli_connect ('localhost', 'root', 'root', 'db') or die ('Error connecting');
$lt = $_POST ['lt'];
$lg = $_POST ['lg'];
$loc= $_POST ['loc'];
$query = "INSERT INTO locations (lt, lg, loc)".
"VALUES ('$lt', '$lg', '$loc')";
mysqli_query ($dbc, $query)
or die ('Error querying');
header('Location: http://localhost:8888/stream.php');
mysqli_close ($dbc);
session_start();
$_SESSION['cl'] = $_POST['loc'];
?>
只要对最后四行重新排序,您就可以开始了:
header('Location: http://localhost:8888/stream.php');
mysqli_close ($dbc);
session_start();
$_SESSION['cl'] = $_POST['loc'];
为此:
session_start(); // even better to put this at the very top of the page
$_SESSION['cl'] = $_POST['loc'];
mysqli_close ($dbc);
header('Location: http://localhost:8888/stream.php');
不要忘了打开登录页上的会话。你应该在页面的开头调用session\u start()
,并且要小心你的查询(既然你已经在使用mysqli,为什么不使用参数化查询呢?)你的代码对SQL注入非常开放,如果你还没有被黑客攻击过,你就会被黑客攻击。将准备好的/参数化的查询与PDO或类似方法一起使用,以完全避免此问题。您应该在重定向之前设置会话…您必须在初始化会话变量后调用header()。
session_start(); // even better to put this at the very top of the page
$_SESSION['cl'] = $_POST['loc'];
mysqli_close ($dbc);
header('Location: http://localhost:8888/stream.php');