I have 3 pages:
我有3页:
- index.php
- 的index.php
- login.php
- 的login.php
- display.php
- Display.php的
index.php
Sets up AngularJS using the ngRoute module to navigate my pages.
使用ngRoute模块设置AngularJS以导航我的页面。
login.php
Loaded by default and sets PHP $_SESSION variables.
默认加载并设置PHP $ _SESSION变量。
display.php
Echos the contents of $_SESSION.
回声$ _SESSION的内容。
I navigate to display.php from login.php using a link setup with ngRoute.
我使用与ngRoute建立链接从login.php导航到display.php。
Problem
display.php does not show $_SESSION variables no matter how many times I navigate to and from it. It will only display them if I manually navigate to the page such as refreshing the page or entering the address in the browser.
无论我导航到多少次,display.php都不显示$ _SESSION变量。如果我手动导航到页面(例如刷新页面或在浏览器中输入地址),它将仅显示它们。
I know the php code is executed because I can echo other things to the screen it just doesn't access the $_SESSION variables.
我知道php代码已执行,因为我可以回显其他东西到屏幕上它只是不访问$ _SESSION变量。
Why is this?
为什么是这样?
4 个解决方案
#1
7
I think i might see where your problem is. You try to access php session in your single page angularJS HTML templates am i right? like:
我想我可能会看到你的问题所在。您尝试在单页angularJS HTML模板中访问php会话我是对的吗?喜欢:
<div ng-repeat="n in <?php $_SESSION['someSessionArray'] ?>">
That is not how it works. Your $_SESSION will never be available in your templates. What you can do, is use an ajax request for your login authentication and have that request give you a session id. Then use that session id when starting your session in further ajax requests (as already mentioned).
这不是它的工作原理。您的$ _SESSION永远不会在您的模板中可用。您可以做的是,使用ajax请求进行登录身份验证,并让该请求为您提供会话ID。然后在进一步的ajax请求中启动会话时使用该会话ID(如上所述)。
Then, when you want to store something to the php session, access the data via ajax request and php service.
然后,当您想要存储某些内容到php会话时,通过ajax请求和php服务访问数据。
a VERY, VERY, VERY, simple Example: inside getFromSession.php
一个非常,非常,非常简单的例子:在getFromSession.php里面
session_start($_GET['session_id']);
$key = $_GET['key']
echo json_encode($_SESSION[$key]);
inside storeToSession.php
在storeToSession.php中
session_start($_GET['session_id']);
$key = $_GET['key'];
$value = $_GET['value'];
$_SESSION[$key] = $value;
inside your login.php
在你的login.php里面
$user = yourAuthMechanism($_GET['username'],$_GET['password']);
if($user) {
session_start();
echo json_decode(array('status' => 'success','sid' => session_id()));
}
else { ... error handling
inside anywhere in your angular where you need to access session data:
在您需要访问会话数据的角度内的任何位置:
$promise = $http.get('pathtoyourphp/getFromSession.php?key=foo');
$http.set('pathtoyourphp/getFromSession.php?key=bar&value=4');
// now use promise to acces the data you got from your service
#2
1
In general, no reason exists, why AngularJS apps, which request PHP-based server-side stuff, won't be able to read $_SESSION.
一般来说,没有理由存在,为什么请求基于PHP的服务器端的AngularJS应用程序无法读取$ _SESSION。
That said, please provide at least the core concepts of of your AngularJS code, so we can provide further details.
也就是说,请至少提供AngularJS代码的核心概念,以便我们提供更多详细信息。
Additionally, put just this in display.php
:
另外,将它放在display.php中:
<?
echo __FILE__
. '<br />' . date( DATE_RFC822 )
. '<br />' . var_dump( $_SESSION )
;
// intentionally skipped dangerous closing PHP-tag
Now run your AngularJS app and tell what comes out.
现在运行你的AngularJS应用程序并告诉它出来了。
#3
1
Make sure you start the session before reading the SESSION variables.
确保在读取SESSION变量之前启动会话。
<?php
session_start();
echo $_SESSION["user9"];
?>
#4
0
I don't think you're looking for angularJS. I think you're looking for something more like this.
我不认为你在寻找angularJS。我想你正在寻找更像这样的东西。
index.php:
index.php文件:
<html>
<header>
<title>Login</title>
</header>
<body>
<form method="POST" action="login.php">
<input type="username" name="username" placeholder="username" />
<input type="password" name="password" placeholder="password" />
<input type="submit" value="Login" />
</form>
</body>
</html>
login.php
的login.php
<?php
session_start();
if(empty($_POST)) {
die("You don't have permission to be here.");
} elseif(empty($_POST['username']) or empty($_POST['password'])) {
die("All fields are required.");
}
$username = "admin";
$password = "password";
if($_POST['password'] == $password && $_POST['username'] == $username) {
$_SESSION['loggedIn'] == "true";
header("Location: show.php");
} else {
die("Invalid login");
}
?>
show.php
show.php
<?php
if($_SESSION['loggedIn'] == "true") {
echo "You are logged in";
} else {
die("You don't have permission to be here.");
}
?>
#1
7
I think i might see where your problem is. You try to access php session in your single page angularJS HTML templates am i right? like:
我想我可能会看到你的问题所在。您尝试在单页angularJS HTML模板中访问php会话我是对的吗?喜欢:
<div ng-repeat="n in <?php $_SESSION['someSessionArray'] ?>">
That is not how it works. Your $_SESSION will never be available in your templates. What you can do, is use an ajax request for your login authentication and have that request give you a session id. Then use that session id when starting your session in further ajax requests (as already mentioned).
这不是它的工作原理。您的$ _SESSION永远不会在您的模板中可用。您可以做的是,使用ajax请求进行登录身份验证,并让该请求为您提供会话ID。然后在进一步的ajax请求中启动会话时使用该会话ID(如上所述)。
Then, when you want to store something to the php session, access the data via ajax request and php service.
然后,当您想要存储某些内容到php会话时,通过ajax请求和php服务访问数据。
a VERY, VERY, VERY, simple Example: inside getFromSession.php
一个非常,非常,非常简单的例子:在getFromSession.php里面
session_start($_GET['session_id']);
$key = $_GET['key']
echo json_encode($_SESSION[$key]);
inside storeToSession.php
在storeToSession.php中
session_start($_GET['session_id']);
$key = $_GET['key'];
$value = $_GET['value'];
$_SESSION[$key] = $value;
inside your login.php
在你的login.php里面
$user = yourAuthMechanism($_GET['username'],$_GET['password']);
if($user) {
session_start();
echo json_decode(array('status' => 'success','sid' => session_id()));
}
else { ... error handling
inside anywhere in your angular where you need to access session data:
在您需要访问会话数据的角度内的任何位置:
$promise = $http.get('pathtoyourphp/getFromSession.php?key=foo');
$http.set('pathtoyourphp/getFromSession.php?key=bar&value=4');
// now use promise to acces the data you got from your service
#2
1
In general, no reason exists, why AngularJS apps, which request PHP-based server-side stuff, won't be able to read $_SESSION.
一般来说,没有理由存在,为什么请求基于PHP的服务器端的AngularJS应用程序无法读取$ _SESSION。
That said, please provide at least the core concepts of of your AngularJS code, so we can provide further details.
也就是说,请至少提供AngularJS代码的核心概念,以便我们提供更多详细信息。
Additionally, put just this in display.php
:
另外,将它放在display.php中:
<?
echo __FILE__
. '<br />' . date( DATE_RFC822 )
. '<br />' . var_dump( $_SESSION )
;
// intentionally skipped dangerous closing PHP-tag
Now run your AngularJS app and tell what comes out.
现在运行你的AngularJS应用程序并告诉它出来了。
#3
1
Make sure you start the session before reading the SESSION variables.
确保在读取SESSION变量之前启动会话。
<?php
session_start();
echo $_SESSION["user9"];
?>
#4
0
I don't think you're looking for angularJS. I think you're looking for something more like this.
我不认为你在寻找angularJS。我想你正在寻找更像这样的东西。
index.php:
index.php文件:
<html>
<header>
<title>Login</title>
</header>
<body>
<form method="POST" action="login.php">
<input type="username" name="username" placeholder="username" />
<input type="password" name="password" placeholder="password" />
<input type="submit" value="Login" />
</form>
</body>
</html>
login.php
的login.php
<?php
session_start();
if(empty($_POST)) {
die("You don't have permission to be here.");
} elseif(empty($_POST['username']) or empty($_POST['password'])) {
die("All fields are required.");
}
$username = "admin";
$password = "password";
if($_POST['password'] == $password && $_POST['username'] == $username) {
$_SESSION['loggedIn'] == "true";
header("Location: show.php");
} else {
die("Invalid login");
}
?>
show.php
show.php
<?php
if($_SESSION['loggedIn'] == "true") {
echo "You are logged in";
} else {
die("You don't have permission to be here.");
}
?>