Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to find the best and cleaner way to execute a javascript function if a PHP session exists.

js-script.js

  • contains javascript function -> function doStuff(){ //do stuff }

index.php

  • contains js file -> in head
  • contains PHP class that check for $_SESSION -> included before DOCTYPE

Please let me know thougts (I'm using PHP, javascript, jQuery).

  1. Best way to execute js script if $_SESSION exist?
  2. Best way to stop js script if $_SESSION is destroyed?

Thanks,

Greg

share|improve this question
You'd have to have some method for the client-side Javascript to query the server-side PHP script. Otherwise Javascript has no way of knowing the session's current status. Remember that JS executes on the client browser, while PHP executes on the server. – Marc B Mar 18 '11 at 16:44
add comment (requires an account with 50 reputation)

4 Answers

<head>
  <? if (session_is_registered('foo')) echo '<script>doStuff();</script>'; ?>
  ...
</head>

Can't cross the server/client boundary, so best-case is to dump something on the page that queues the client to fire the javascript function.

Alternatively, you could have something like session_verify.php that you can reference in an AJAX call, then "continue executing"/"stop" based on the response you receive back.

Pseduo code:

if (ajax_response_from_session_verify() == still_logged_in){
  execute doSomething();
else
  stop doSomething();

Keep in mind though, that calls to the session check file could potentially prolong the session depending how you're using it. (I'm not sure if that's desired)

share|improve this answer
I would have said exactly what Brad has said here. If this doesn't help, then let us know specifically what you are trying to do and we may be able to help more? – jaz9090 Mar 18 '11 at 16:46
First option Brad gave is actually the way I do it. I guess there isn't really a more elegant way. Now that session has started and JS is executed what would be the best way to remove the JS if session is destroyed (user logged out onclick to a link)? I thought of refreshing the browser -> either with PHP header or HTML meta refresh. – Greg Mar 18 '11 at 17:03
@Greg: It really all depends how you have this setup. With your wording, I'm assuming that the logout is an AJAX-based request. So, if you code the script modular enough, you can have the reply quot the execution/unbind anything it needs to (basically perform a local cleanup). If it isn't AJAX (or as a general rule) a page refresh is probably requires the least amount of effort. – Brad Christie Mar 18 '11 at 17:26
add comment (requires an account with 50 reputation)

You have two options.

  1. Have the function in a separate is file and include it only if the session is set.
  2. Serve the JS file as a PHP file. This will allow you to run PHP commands in your JS file. Just make sure your content type is ok.
share|improve this answer
add comment (requires an account with 50 reputation)
if(session is OK) {
    $script = "doStuff();";
}

then, in your HTML head:

<head>
...
<script type="text/javascript">
document.ready(function() {
<?php echo $script; ?>
});
</script>
...
</head>
share|improve this answer
add comment (requires an account with 50 reputation)

Brad is correct that you would need to use PHP to output to the page since you can't directly call JS from the server.

An alternative to writing out script tags is to place a flag in your HTML via PHP, then use JavaScript to read that flag. For instance, you are using jQuery, you may do something like this:

<div id="sessionExists><?php if (isset($_SESSION['foo'])) echo 'true';?></div>

And in your jQuery:

var session = ($('#sessionExists').text() === 'true'))? true : false;

Now you have a JS variable that holds a boolean for "sessionExists," which you can query in JS scripts and act accordingly.

share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.