• AngularJS ngRoute and PHP $_SESSION variables



    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章学习小结
    数据结构-第4章学习小结
    数据结构-第3章学习小结
    数据结构-第2章学习小结
    数据结构-第1章学习小结
    实验五 单元测试
    实验四 代码评审
    实验三 UML建模工具的安装和使用
    实验二 结对编程 (第二阶段)
    结对编程实验 第一阶段
  • 原文地址:https://www.cnblogs.com/ztguang/p/12645639.html
Copyright © 2020-2023  润新知