在我检查登录数据是否正确后,我不知道如何访问主页index.php并显示一些带有javascript函数的div。我在用户点击提交按钮后运行login.php scrip,而如果用户数据正确,我想用javascript显示一些div并保留在index.php页面上。我尝试使用javascript函数和php中的标题(位置:./ index.php),但它没有用。
我的代码:
if($name == $username && $pass == $password){
echo '<script type="text/javascript">'
, 'showDiv();'
, '</script>';
header("Location: ./index.php");
}
答案 0 :(得分:1)
将session_start();
放在login.php
和index.php
文件的顶部,然后将其放在login.php
文件中:
if ($name == $username && $pass == $password) {
$_SESSION['authenticated'] = true;
header("Location: ./index.php");
}
然后,在index.php
文件上,执行此操作以查看用户是否已登录:
if (isset($_SESSION['authenticated']) && $_SESSION['authenticated']) {
echo '<script type="text/javascript">'
. 'showDiv();'
. '</script>'; # If you want your div to show on your index.php
}
答案 1 :(得分:0)
你使用的方式似乎很奇怪。更好的方法是使用SESSION变量来存储登录信息(当用户登录时)并在检查SESSION变量后显示内容,如下所示: -
if(isset($_SESSION['somevalue']))
{
echo 'whatever you want';
}
答案 2 :(得分:0)
在你的login.php中写下这个。
session_start();
if($name == $username && $pass == $password){
$_SESSION['somevalue'] = $username;
header("Location: ./index.php");
}
在index.php中您需要检查您的会话是否已设置?如果使用javascript代码设置show youw div。
在session_start();
if(isset($_SESSION['somevalue']))
{
//show your div code here.
}
答案 3 :(得分:0)
if($name == $username && $pass == $password)
$_SESSION['login'] = true;
现在代码首先启动会话并检查登录名会话变量。
session_start();
if (isset($_SESSION['login'] ) && $_SESSION['login'] === true)
{
//do what logged in user require
}
else
{
// redirect them to login page with proper error.
}
答案 4 :(得分:0)
你必须写session_start();在login.php文件的第一行。因为有一段时间它有问题。
session_start(); // line 1 in login.php
if($name == $username && $pass == $password)
{
$_SESSION['mydata'] = $username;
header("Location: ./index.php");
}
答案 5 :(得分:0)
实际上,您需要使用一些Jquery和Ajax调用,以便在用户单击提交按钮后不刷新页面。
您需要的代码是这样的:
$.ajax({
'url': 'http://yourdomain.com/authentication_script.php',
'beforeSend': function(xhr) {
xhr.setRequestHeader("Authentication", "Basic " + encodeBase64(username + ":" + password)
},
sucess: function(result) {
alert('authenticated'); // here you can add some other functions to execute once authenticated
}
});
您的想法是,您应该注意,在身份验证脚本中,您还应该包含会话变量,并在您/他想要/ s时跟踪用户数据。