0

How to unset session in php, via angular js this is what i hve,

app.js

  $scope.ths=function () {
$http.get('ajax/logout.php', function (response) {
});
 //logout.php
session_start();
$_SESSION['user'] = "";
iam
  • 19
  • 3
  • You call the PHP script (as you're doing) and then unset the session in the PHP script? What exactly is it that doesn't work/you're having issues with? – M. Eriksson May 08 '18 at 13:36
  • So are you getting any errors or something? What's not working? – Andrei May 08 '18 at 13:38
  • i'm getting something thats not working – iam May 08 '18 at 13:38
  • That's what we're asking. What exactly is not working? Are you getting errors? Does the ajax go thru? Is the session not empty after? Be specific. – Andrei May 08 '18 at 13:40
  • possibly with `unset($_SESSION['user'])` – Aleksey Solovey May 08 '18 at 13:41
  • session becomes empty after refreshing the browser – iam May 08 '18 at 13:42
  • The session get's empty straight away (on the server), but the front end is already loaded (in the client) and doesn't know anything about what happens on the server. When you've logged the user out, you could redirect the user to the login form or something (a real redirect) or you need to change the user state in your code to say it's logged out. – M. Eriksson May 08 '18 at 13:44
  • you mean session_destroy ? http://php.net/manual/en/function.session-destroy.php – ahammar May 08 '18 at 13:45
  • thanks for the explanation @Magnus Eriksson now i hve got it. – iam May 08 '18 at 13:50
  • You should check how to maintain session in php with angular js. This question might help you. https://stackoverflow.com/questions/14957450/maintaining-session-through-angular-js – Dhruv Kapatel May 08 '18 at 14:39

2 Answers2

1

Use following to remove a variable from session

unset($_SESSION['user'])

if you want to logout completely you can also use Session Destroy method

0

PHP is a server side language while JavaScript and all its related frameworks built in JavaScript are client side, as Session are managed by Server so there is no direct connection between JavaScript and PHP to clear or manage your sessions however,

You can do a trick by creating a php file where you can unset all your session and call it asynchronously.

hope this will help.

Good luck.

  • If you look at the code (and read the comments), you'll see that this is what the OP already does. – M. Eriksson May 08 '18 at 13:46
  • my bad, didn't noticed that, but yeah that's what I did explained him, i reckon he is having issues in the logout.php file, first he is not un-setting the sessions correctly, which @ Aleksey has already explained. If this still even didn't worked, in worse case @iam you should first check and run the logout.php page to see what it does return in the browser, it should be an easy fix! Cheers mate ;o) – Waqar Hussain May 08 '18 at 14:02