Jump to content

php sessions in a js file?


blueman378

Recommended Posts

hi guys, well i have a .js file that is included into a php page through the <script src="blabla.js></script> but i was wondering is there anyway to have session variable affect something inside the .js file?

 

like a if statment inside the js file? i could include 2 different files based on this but i would prefer to keep it small this way

 

Link to comment
https://forums.phpfreaks.com/topic/101828-php-sessions-in-a-js-file/
Share on other sites

Can't be done.

 

The only way I could see this working if you turned the js file into a PHP file, i.e. echo out the JavaScript within the PHP file.

 

For example:

<?php
  require ('javascript.php');
  //
?>

 

And in javascript.php you would do something like this:

<?php
echo "<script type='text/javascript'>";
echo "function foo(){"
  if (condition) echo "alert('javascript function');"; //echo out the javascript
echo "}"; //end foo
echo "</script>"
?>

It can be done. There are two ways, and they are both basically the same with one small difference. The first is rename your javascript file with a .php extension. Then link to it like this:

 

<script src="blabla.php" type="text/javascript"></script>

 

In the other way, you leave the extension name as .js, but you will have to configure your server to process .js files as .php. You can do it like this:

 

Ok, so for the sake of keeping this fun,

  we\'re going to use ".ass" instead of ".php". The first thing

  we\'ll need to do is setup Apache Web Server to allow us to use this "ass"

  that we\'ve created. Are you laughing yet? I hope so. Let\'s open up the Apache

  httpd.conf file and go to the area where we have added AddType application/x-httpd-php

  .php and just add a .ass behind it. It should look like this:

AddType application/x-httpd-php .php .ass

Once you have achieved entering your "ass" into the

  httpd.conf file, restart apache. Usually it could be "httpd restart"

  or "apachectl restart", it all depends on your system.

Now, once we rename all of our PHP scripts to ".ass"

  instead of ".php" you\'ll be able to call up http://yourserver/my.ass

  and it will run just like it was if it were named .php.

 

(use .js instead of .ass). Excerpt taken from this thread.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.