• 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.");
        }
     ?>

  • 相关阅读:
    通过哪吒动漫豆瓣影评,带你分析python爬虫与BeautifulSoup快速入门
    带着canvas去流浪系列之九 粒子动画
    带着canvas去流浪系列之八 碰撞
    Python小数据保存,有多少中分类?不妨看看他们的类比与推荐方案...
    免费试用 | 多模 NoSQL 服务GeminiDB for Cassandra 全球首发
    Vue+ElementUI项目使用webpack输出MPA
    nmon
    补习系列(12)-springboot 与邮件发送
    从React 编程到"好莱坞"
    百度网盘API的操作--PCS 百度个人云存储 上传 ,下载文件
  • 原文地址:https://www.cnblogs.com/ztguang/p/12645639.html
Copyright © 2020-2023  润新知