Angularjs Ngroute and PHP $_session variables____php

Source: Internet
Author: User
Tags error handling session id php session sessions


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


I I am I might the where your problem is. You are try to access PHP sessions in your single page angularjs HTML templates am I right? Like

<div ng-repeat= "N in <?php $_session[' Somesessionarray ']?>" >

It works. Your $_session would never be available in Your templates. What can do, are use an AJAX request for your login authentication and have that request give your a session ID. Then Use this session ID when starting your sessions in further AJAX requests (as already mentioned).

Then, when you are 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 (' status ' => ' success ', ' Sid ' => session_id ());
}
else {... error handling

Inside anywhere in your angular where your 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 for your got from your service

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


I don ' t have to do you ' re looking for angularjs.i to have a ' re looking for the something more like this.

index.php:

 

login.php

<?php
   session_start ();
   if (empty ($_post)) {
      die ("you don t have permission to is 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"; C10/>header ("Location:show.php");
   } else {
       die ("Invalid login");
>

show.php

<?php
    if ($_session[' loggedIn '] = = "true") {
        echo "You are logged in";
    } else {
        die ("Don" t have Permission to is here. ");
 >

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.