zoukankan      html  css  js  c++  java
  • 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.");
        }
     ?>

  • 相关阅读:
    markdown keynote
    pyecharts
    运行成功
    python发邮件3
    python发邮件2
    python发邮件1
    python发邮件
    python中的编码声明
    auther tonyxiao
    111
  • 原文地址:https://www.cnblogs.com/ztguang/p/12645639.html
Copyright © 2011-2022 走看看