ajax登录问题

发布于 2024-08-25 05:51:02 字数 154 浏览 1 评论 0原文

我创建了登录页面,它将 ajax 请求发送到 php 页面以进行登录验证。 在该 php 页面上,我创建会话,并根据登录验证发送响应。 如果用户通过身份验证,我将其从发送ajax的java脚本重定向到主页。但在那个主页上我无法获取该会话对象...为什么?你能告诉我在主页上检索该会话的解决方案吗

i have created login page which sends ajax request to a php page for login verification.
On that php page i m creating session, and sending response as per login verification.
if user is authenticated i m redirecting it to home page from java script from where i send ajax. but on that homepage i cant get that session object... why? can u tell me solution to retrieve that session on home page

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

送舟行 2024-09-01 05:51:02

我不确定我这样做的方式是否正确,但它对我有用(我希望我没有忘记任何事情):

这是我的login.php所做的:

header('Content-type: text/json');

// here : import files I need

session_start();

// here : load some parameters into session variables
// here : init mysql connection
// here : get user and password from $_POST and check them against the database

// If the user can't connect, return an error to the client
if ( ! $ok )
{
    echo '{ "ok": "N" }';
    exit;
}

$_SESSION['user']    = $user;

echo '{ "ok": "O" }';

?>

然后,当我访问另一个 php 文件时,它是这样开始的:

header('Content-type: text/json');
// again, required files go here

session_start();

if ( ! isset($_SESSION['user'] )) {
        echo '{ "ok": "N" }';
    exit;
}

$user=$_SESSION['user'];
....

我进行的每个 ajax 调用都会检查结果是否告诉我用户未连接,如果没有连接,则返回到登录页面。

  $.ajax({
    type: "POST",
    url: "myphp.php",
    dataType: "json",
    data: somedatatopost,
    success: function(pRep){
      if (!pRep) {
        alert("No response from the server.");
        return;
      }
      if (pRep.ok=="N") {
        window.location = '/index.html';
        return;
      }
      // here is where I handle a successful response
    }
  });

对于登录 ajax 调用,我有:

[....]
// here is where I handle a successful response
window.location='mynewpage.html' 

I am not sure I do it the right way, but it works for me this way (I hope I didn't forget anything):

Here's what my login.php does :

header('Content-type: text/json');

// here : import files I need

session_start();

// here : load some parameters into session variables
// here : init mysql connection
// here : get user and password from $_POST and check them against the database

// If the user can't connect, return an error to the client
if ( ! $ok )
{
    echo '{ "ok": "N" }';
    exit;
}

$_SESSION['user']    = $user;

echo '{ "ok": "O" }';

?>

Then when I access another php file, here's how it begins :

header('Content-type: text/json');
// again, required files go here

session_start();

if ( ! isset($_SESSION['user'] )) {
        echo '{ "ok": "N" }';
    exit;
}

$user=$_SESSION['user'];
....

Every ajax call I make checks if the result tells me the user isn't connected, and returns to the login page if he isn't.

  $.ajax({
    type: "POST",
    url: "myphp.php",
    dataType: "json",
    data: somedatatopost,
    success: function(pRep){
      if (!pRep) {
        alert("No response from the server.");
        return;
      }
      if (pRep.ok=="N") {
        window.location = '/index.html';
        return;
      }
      // here is where I handle a successful response
    }
  });

For the login ajax call, I have :

[....]
// here is where I handle a successful response
window.location='mynewpage.html' 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文