将参数从一个HTML页面传递到另一个HTML页面 [英] Pass arguments from one HTML page to the other
问题描述
我从一个HTML页面获取参数到另一个HTML页面有问题。情况是这样的:
我使用PHP和Ajax / jQuery来构建这个网站,第一页是登录表单。当有人点击按钮登录时,我发送POST请求与ajax到一个PHP脚本,将在数据库中搜索此用户/密码(我使用ajax,因为如果发生错误,我会显示一条消息无需重新加载页面)。当PHP脚本完成时,它会将用户标识返回到登录页面。
如何从登录页面转到另一个页面(例如,主页)并保持用户标识在此处使用。我知道如何使用location.href来解决这个问题,但我不想在URL中显示用户标识。
谢谢。
I am having a problem to get arguments from one HTML page to the other. The case is this:
I am using PHP and Ajax/jQuery to build this website and the first page is a login form.
When someone click in the button "login" I am sending a POST request with ajax to a PHP script that will search in the database for this user/password(I am using ajax because if an error occurs I will show a message without reload the page). When the PHP script finishes, it returns the user id to the login page.
How can I go from the login page to another page(e.g. Home) and keep the user id to use there. I know how to resolve this using location.href but I don't want to show the user Id in the URL.
Thank you.
推荐答案
尝试使用会话:
<?php
session_start(); //u have to have this on the top of your page, to be able to use session
$_SESSION["user_id"] = $user_id;
session_destroy(); //u can use this to unset the variables stored in the session. use only after u are done using with the $_SESSION.
?>
访问$ _SESSION变量(就像post和get):
to access the $_SESSION variables(just like post and get):
$variable = $_SESSION["user_id"];
一旦你设置了会话,你可以在你的任何页面中使用它们,就像全局变量一样,只要您将session_start()放在页面的开头
once u set the session u can use them in any of your page just like a global variable and as long as you put session_start() at the beginning of the page
这篇关于将参数从一个HTML页面传递到另一个HTML页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!