Include server-side javascript in PHP - php

i'm converting in PHP5 a quite old ASP project that was using a lot of server-side javascript.
is there a way to use that code or i'll have to rewrite everything in PHP?
EDIT
this is how the code is embedded in ASP
<!-- #include file=helper.js -->
snippet of my helper.js
<script language=javascript runat=server>
//here are the functions
</script>
then in ASP is see that functions are called as usual ie
response.write myJsfunction()
btw, if i have to rewrite the code, i'll do on my own, not using any software

The answer is "you need to rewrite everything in PHP" PHP does not as is support Javascript on the server-side in reuseable manner. Also, the automatic code transformations are very klunky: clean up work takes more time than building everything by hand from the beginning.
However, you can run server-side Javascript code with tools like node.js and backbone.js. Mixing with PHP code is no-go though as these projects will provide their own, non-PHP-compatible, web-servers.

I presume you mean jscript? Have you tried asp2php?

Related

What is the best way to include a navigation bar in all pages?

I am thinking a way to add navigation bars to all of my webpages, and when I update the file, all pages should also update.
I researched on Google for a bit of time, what I found are:
 PHP 
 SSI  (I don't even konw what's this)
 JavaScript 
But which one is the best? And can someone explain what SSI is?
SSI (Server-Side includes) are outdated. I would suggest not using them. JavaScript will work, but your browser would have to download an additional file just to construct the page. Also, people with JavaScript disabled would not be able to navigate your site. It really doesn't make sense to use JavaScript to construct your page, so don't use it unless you want special effects.
PHP is the best option. You just have to include the navigation script at the top of your page like include_once('nav.php');.
PHP
PHP would work fine, but using it would mean you'd have to set things up a bit differently, including doing things like:
Running PHP on your server
Renaming your .html files to .php
Changing your links to point to the .php files
Learning at least enough about PHP to use require
SSI
SSI (Server Side Includes) are some ancient thing that nobody uses. Don't use them. But if you're curious, take a look at mod_include.
Javascript
Client-side javascript could sort of be made to work, but you're better off doing this kind of thing on the server.
Here are some other options to consider.
Another server-side language
PHP isn't the only server-side language. You might prefer python, ruby, server-side javascript, etc.
XSLT
XSL transformations are well-suited to this kind of templating, and can be applied either by the browser or on the server. Support is good across browsers, and has been for some time. Extensions like "noscript" tend to break XSL transformations in some browsers, so applying them on the server is usually preferred when possible.
iframe hack
You could simply put your navigation in a separate HTML file and include it in an iframe on every page. Only use this as a last resort. If you cannot use a server-side language, cannot or do not want to use XSLT, and have relatively few pages, this might work okay. Otherwise, don't do it.
But which one is the best It depends on what you are already using on your website.
If it written on php then it is better to use php for the inclusion. By simple include('file_with_nav_bars_code'); at the right place in your template or code you are able to insert the static html code or the generated one by the file mentioned above.
SSI (server-side includes) is good for the 'static' pages. It is like a simple scripting which can be done by the server after some setup. If you check templates of the error pages of the Apache server you will see SSI directives there, which include the footer, header, outputs some basic information like time, requested URL and so on. Inclusion of the some code will look like <!--#include virtual="/file_with_nav_bars_code" --> and in the same way you could include even some CGI script <!--#exec cgi="/cgi-bin/example.cgi" --> Read more here http://httpd.apache.org/docs/2.0/howto/ssi.html and here http://en.wikipedia.org/wiki/Server_Side_Includes
The javascript is also good for static pages. You have to load javascript code which will 'generate' the html code you need and output it at the place on the page you want. It has one serious negative effect - search engines will not be able to follow the links generated by js as they will not run it (as the visitors with js turned off, but I do not know such cases. Even Opera mini is able to render the results of the simple js output). The simple way is to include the js script at the place you want to have a nav bar:
<script src='nav_bar.js'></script> and in nav_bar.js you will (as a very simple example) something like this: document.write('some html code of the bar here, with escaped new lines and quotes');
ps: For many years of experience I've tried all of them :) I used js and SSI for the early static pages, now I'm only using php and other server-side scripting languages. You can still use javascript with php generated pages, but for the SSI you have to tell the webserver to pass the output of the php one more time looking for SSI directive instead of returning it back to the browser.
SSI stands for Server Side Includes which is the concept that means your files will all include the same file (for the navigation bar for example), so when you make any changes in that file, it'll get reflected in all your pages.
PHP and javascript are two different things, PHP is a server-side language while javascript is a client-side language.
If you just want a navigation bar, PHP will be enough. But if you want to add any client-side funcionalities like a popup menu under sections and things like that you'll have to use javascript too.
SSI still works on modern servers using the v function or % (tested on Apache/2.4.18).
Example (nav include):
<!--#include file="nav.html" -->
Example active menu item (if test on document name):
<!--#if expr='v("DOCUMENT_NAME")=~/about.html/'-->
<a class="active" href="#">
<!--#else -->
<a href="about.html">
<!--#endif -->
About</a>
Example 2 (if test on document path):
<!--#if expr="%{DOCUMENT_URI} =~ /product/"-->
Product path
<!--#else-->
Some other path
<!--#endif-->
.htaccess to get SSI working (example):
AddType text/html .shtml
AddHandler server-parsed .html
AddHandler server-parsed .shtml
Options Indexes FollowSymLinks Includes
Older Apache version use different expr syntax.

php communicate with html and javascript

I heard people said php server site script cannot communicated with html and javascript client-side script. But when I test it, seem php can tell html and javascript what to do. Here is my codes :
<?php if ((isset($username1))&&($username1 == $username2)){ ?>
<div style="position:relative;">
<img src="http://plekz.com/images/layouts/theme.png" onClick="showThemeDiv(); hideThemeTip();" style="margin-bottom:3px; margin-left:1px; position:relative;" onMouseOver="showThemeTip();" onMouseOut="hideThemeTip();" />
</div>
<?php } ?>
I tested it on IE, Firefox and Chrome, all works perfectly. But I still worry code this way will lead to problem when I move all the file to other online webhosting/server... Do I need to put all html and javascript codes into php echo? Or I can just code like this without having any problem in future? Is it standard way to code like this?
What you are doing is just PHP, in your code there is no actual communication between "html and javascript", which is on the client's side. And PHP, which is on the server side.
What people mean with communication is that the user can change something on the webpage dynamically without reloading the webpage. Such a thing can be done using AJAX.
In short, the code you are using will work on any webserver and with any browser.
It's a bad way when html and php code are in one file and much better when separate. But you can code like this and should not put all html to php function 'echo'. It's the same instead you don't need to trigger the php function. And more much understandable to read a code, IMHO.
PHP is simply a scripting language which gets interpreted at runtime whenever a request is made; people say it cannot communicate with HTML/Javascript because it is interpreted on the server side, it is not visible/accessible to the user agent once the page is rendered.
It is however possible to communicate with PHP scripts through AJAX calls.

Inline PHP in a .JSP?

I am relatively new to web-development and am encountering an issue using some inline PHP code.
The page is a JavaServer Page (.jsp) and I am trying to implement a JFormer form.
When I add my JFormer PHP code to my .jsp page, it just displays as plain-text and refuses to cooperate with me (even when using demo code from the site's documentation). Is this because of some sort of incompatibility between using PHP on a .jsp page?
If that is the case, what are some work-around that I could use? Should I use an iframe?
I need to preserve the use of the .jsp page and would prefer very much to use JFormer, but if I have to I can toss it.
Example of something similar to what I am doing can be found at: http://www.jformer.com/documentation/getting-started/installation/
JSP and PHP are both server-side languages. As such, all scripting in a given file must be processed by the required engines on the server to produce the necessary HTML output.
I suppose it is possible to rig multiple engines inline to process first JSP, then PHP, but that seems cumbersome and error prone.
Instead, consider using an iframe (as you suggested) or load the PHP content via an AJAX call.
PHP is executed by a PHP interpreter and output HTML. JSP is compiled and executed by a Java VM, and output HTML. You can't execute PHP inside JSP code (and vice-versa). It's like if you put Chinese words inside an English speech. Nobody can understand.
I think the point of this is that the examples for jFormer use PHP for the server side logic. If you want to integrate jFormer into your JSP project, learn how to code the equivalent PHP functionality in JSP. You may need to create a Servlet for portions of the logic.
It looks like JFormer requires PHP so you can't make this work on a JSP page easily. You can rewrite the JFormer PHP code in Java/JSP but this may be a lot of work.
The container (like Tomcat) you're using may be able to run PHP scripts as CGI scripts. If you do this you can't easily share session information between PHP and Java. Javascript could be used to accomplish this, but beware of security issues. If you still want to use JSP you could make an iframe that points to the PHP page, as you said.
Here's an article on setting that up for Tomcat:
http://wiki.apache.org/tomcat/UsingPhp
Disclaimer: I don't know JFormer.

Can I execute js files via php?

The situation is next:
I have php file, which parses a web-page. on that web-page is a phone number and it's digits are mixed with each other. The only way to put each digit on the correct place is to use some JS functions (on the client side). So, when I execute that php file in linux console, it gives me all that I need, except js function's result (no wonder - JavaScript is not a server-side language). So all I see from JS - only a code, that I have written.
The question: can I execute js files via php and how?
Results of a quick google search (terms = javascript engine php)
J4P5 -- not developed since 2005 [BAD](according to its News)
PECL package spidermonkey
a 2008 post by jeresig points to PHPJS but can't see when it was last updated.
I'm sure you'll find many more links on that google search.
Alternatively:
you say that the digits are scrambled and you need to "unscramble" them using js. Can you code that unscrambling logic into a PHP function and just use it? Will sure save you a lot of trouble, but if learning to use js in php is what you're after, then its a whole different story...
Zend tutorial: "Using javascript in PHP with PECL and spidermonkey"?
http://en.wikipedia.org/wiki/Comparison_of_Server-side_JavaScript_solutions
Alternatively, PHP has simple functions for executing other programs and retrieving their output (I used this along with GPG once to create a PHP file manager which could live-encrypt files as you were uploading them and live-decrypt as you were downloading them)
Using those functions along with http://code.google.com/p/v8/ you should be able to interpret any javascript.
Not unless you know someone who's implemented a Javascript engine in PHP.
In other words, probably not.
Without some sort of browser emulation or passing the unparsed js off to a server side implementation of javascript (maybe node.js?), you won't be able to execute it.
However, does the page use the same js function to unscramble the phone number every time? You should be able to read the incorrect digits and shuffle them with PHP.
If you're prepared to do a bit of work building your own JS runtime to work with it, Tim Whitlock has written a javascript tokenizer and parser in pure PHP
node.js is server-side... but full JS :) no PHP in it so I don't it answer your needs...
Anyway, here is an example : a chat in JS both client & server-side : http://chat.nodejs.org/
Plus, not every host allows you to use the v8 engine...
If you have Javascript data objects, and you need to convert them to/from PHP arrays, that's quite easy using PHP's json_encode() and json_decode() functions.
But actually running Javascript code? No. You can't. You might be able to find a JS interpreter written in PHP (a few other answers have pointed a links that may or may not help you here), or more likely execute the JS using a stand-alone JS interpreter on your server which you call out to from PHP. However if the JS code includes references to the browser's DOM (which is highly likely), that's a whole other set of issues which will almost certainly make it impossible.
Given the way you describe the question, I'd say the easiest solution for you would just be to re-implement the JS code as PHP code; it's unlikely that all the work arounds being suggested would be appropriate for what sounds like a fairly simple bit of utility code.

Clean up PHP/HTML pages

Does anybody know of a good tool that cleans up files with php and html in it? I've used Tidy before but it doesn't do a good job at leaving the php code alone. I know there are various implementations of tidy but does any tool reign champion specifically for pages with html and php?
Cleaning your code starts with separating PHP from HTML !
I am aware that this is a pretty old question but still a valid one. I currently use this and it seems to be doing a decent job: PHP Formatter
For HTML, CSS and JS, DirtyMarkup is a handy tool. Only drawback of these is that you have to copy and paste the code twice.
As far as I know, Tidy is the "reigning champion" when is comes to cleaning html code. The only other tool I've personally used in cleaning code is within Adobe Dreamweaver.
I would agree with seperating your HTML and your PHP code. However, I think you have to think of it kind of backwards. I would seperate your HTML code from your PHP code. Take your HTML and block it up and use include 'html_code_1.php';. Thus you can run Tidy on your HTML and not worry about it affecting your PHP code.
I previously had this problem, however had issues with other programs reorganizing what I coded, and trying to clean it up usually ended up doing more harm than good. To solve this, I am starting to learn the ins and outs of Code Igniter, a basic PHP framework that uses the MVC approach to splitting HTML and PHP. I haven't tested much, but it looks like much less hassle than writing HTML and PHP straight into the single file.
You can use this PHP class, if you can't install the "Tidy" module (sometimes when you buy hosts you can't).
http://www.barattalo.it/html-fixer/

Categories