AngularJS ngRoute and PHP $_SESSION variables____PHP

來源:互聯網
上載者:User


http://stackoverflow.com/questions/22085068/angularjs-ngroute-and-php-session-variables


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:

<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).

Then, when you want to store something to the php session, access the data via ajax request and php service.

a VERY, VERY, VERY, simple Example:inside getFromSession.php

session_start($_GET['session_id']);$key = $_GET['key']echo json_encode($_SESSION[$key]);

inside storeToSession.php

session_start($_GET['session_id']);$key = $_GET['key'];$value = $_GET['value'];$_SESSION[$key] = $value;

inside your 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

+++++++++++++++++++++++++


I don't think you're looking for angularJS.I think you're looking for something more like this.

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

<?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

 <?php    if($_SESSION['loggedIn'] == "true") {        echo "You are logged in";    } else {        die("You don't have permission to be here.");    } ?>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.