Write to server: using text file with HTML5 Jquery/Javascript - javascript

I was wondering if it is possible to write to a server and storing it in a text file using Javascript/Jquery, HTML5. The purpose is, I am making a javascript/jquery game and I wanted to store the scores, without using any server-side language (php, asp, etc.)
Thank you so much for the help, your time is much appreciated.

HTML, JS, CSS, all that is client-side. Meaning it runs in the browser and that's that.
PHP & ASP are server-side, meaning the code gets executed on the server and the result gets sent to the client.
You need a method of communication between the client and server. You can send requests using JS (AJAX) but you'll need something on the server to receive and do something with them. For that you need some server-side code.
So no, you can't update data on a server using only client-side technologies. You can, however, use client-side scripts to communicate with a server and tell the server-side code to update data.

Related

How to run PHP script actively on client side / client machine?

I'm trying to run a PHP script directly on the client side rather than taking request from a client to server & receiving back processed output or file. I created a PHP script which is running properly on the server side. I want to run that same PHP script on the client side as well. The PHP is not installed on the client side, it is only on the server side. Using <object> method I somehow managed to execute a PHP script on the client side. But it's just window or small frame that is visible on the current browser or in a page on the browser of a local machine (or client machine). So whenever PHP functions are invoked, it is executed at the server side, not on the client side.
Is there a way to execute PHP functions actually on the client side as well?
OR
Is there a way to open that entire working PHP page on the client side rather than on small window or in a frame of server page on a client browser without PHP installation?
What I tried is:
I created PHP script as "SERVER.php" which displays the name of all the files of a current working directory of the SERVER machine.
<?php
exec('dir', $status, $result);
var_dump($status);
?>
I want to get name of all the files of a current working directory but of CLIENT machine.
I created a html file as "CLIENT.html" that consists of the following code:
<object type="text/html" data="http://192.168.0.110:81/file2v2.php" width="800px" height="600px" style="overflow:auto;border:5px ridge blue">
</object>
Basically, the object function used in html contains the ip address followed by the port no. (of the Apache Server) and the php file name of the server (where php is installed and located).
Can anyone please help me how to execute PHP functions or say entire PHP scripts on the client side, or the way it can get it done.
All clients where you want to execute PHP scripts need to have PHP installed, as well as a running webserver (if those scripts are accessed via http). There is no way around that.
One way to do it without client side code is to give your webserver SSH access to all the clients. (This means creating the user on each machine and giving it the necessary permissions). Then the server could read the visitor's IP, connect to it via SSH and read the filesystem of the client and display it in the browser. This would be a wild approach, but possible.
Check this out: https://kvz.io/blog/2007/07/24/make-ssh-connections-with-php/
It would also mean that if someone gets access to that server or can somehow exploit it, all clients are compromised as well.
To run a script, you need an interpreter installed, the browser can read js script, but cannot read php script, for example.
https://en.wikipedia.org/wiki/PHP
I think you should take a look at the difference between client-side and server-side. Take a look here, it explains a lot.
Quote from the above link:
Key Differences Between Server-side Scripting and Client-side Scripting
Server-side scripting is used at the backend, where the source code is not viewable or hidden at the client side (browser). On the other hand, client-side scripting is used at the front end which users can see from the browser.
When a server-side script is processed it communicates to the server. As against, client-side scripting does not need any server interaction.
The client-side scripting language involves languages such as HTML, CSS and JavaScript. In contrast, programming languages such as PHP, ASP.net, Ruby, ColdFusion, Python, C#, Java, C++, etc.
Server-side scripting is useful in customizing the web pages and implement the dynamic changes in the websites. Conversely, the client-side script can effectively minimize the load to the server.
Server-side scripting is more secure than client-side scripting as the server side scripts are usually hidden from the client end, while a client-side script is visible to the users.
If you want every logic done on the client-side, take a look at Angular, VueJS or just basic Javascript. But with Javascript (including Angular/VueJS/Etc.) you can not manipulate client-side files, which would be a major security issue if it was able to. To change things on the server-side, you will always need a back-end programming language like PHP, C#, Java etc.
Be aware that everything you do on the client-side, is visible for the client and can be changed by the client. Which makes your application vulnerable for attackers.
To answer your question: You can't.
Basically, no.
But you can read about C# Blazor approach (https://dotnet.microsoft.com/apps/aspnet/web-apps/blazor) and try to implement something like this.

Can AJAX and JavaScript be used purely for server side scripting?

I am just learning php. My approach to learning is that I start analysing the code in a language I am more familiar with.
Hence, I am trying to replicate a PHP dashboard with PHP features like role management, session logging etc.
As I am halfway through it, I find that using ajax , though not primarily meant for it, I can talk to a server.
Using Http verbs, I can also make modifications.
Hence, can JavaScript and Ajax together act as a server side scripting language on its own?
My understanding is a server side scripting language just interacts with the server.
As ajax does the same using JavaScript and http verbs .
Can it essentially replace PHP?
I did a little research, and found server side scripting languages have their own pros like filtering, jQuery and Ajax can use less bandwidth etc.
But everything seems vauge.
Hoping to get a better understanding.
Thank you:)
Server side scripts run at the server, not at the "client" (browser). Javascript run at the browser. Ajax is a feature of JS that allow you to interact with a server without doing a full POST back to that server (changing the page you are actually seeing). During an AJAX call you can interact with whatever is on the server side (PHP, .NET, etc) There are even some sites that have multiple server side scripts.

With JavaScript is it possible to Read/Write from/to a file on the server

I have a series of JSON Objects I want to save locally on my server. I am attempting to avoid any server-side script like PHP as required per demand of whats being built. I know its a security risk, but that in this case is not a particular worry. So that said is it possible to actually write to a file and or get its contents via javascript with or without the help of a lib such as jquery?
I should mention I am attempting to avoid ActiveX as I know this is an IE only feature and the software we are developing is planned to be Cross Browser supported
So that said is it possible to actually write to a file and or get its contents via javascript with or without the help of a lib such as jquery?
Nope. You will need something running on server side that can receive your JavaScript input and write it to the server.
Internet Explorer's proprietary file writing functionality is for writing local (client-side) files only.
You can read a file using ajax, but without a server side language you cannot write a file to the server.
https://developer.mozilla.org/en/ajax
No. Javascript runs on the client. You need server-side code to access the server's file system.
Client-side JavaScript can only send data to a server, there's no way for it to tell the server what to do with the data.
To save data to a file or db on a server, you'll require a server-side script of some sort (could be server-side JS with Node.js). If all you need is persistent data, you could store some JSON strings in localStorage or in cookies as needed. They wouldn't be shareable that way though.
Yes, you can use AJAX requests in JavaScript without using jQuery. However, jQuery will save you an ungodly amount of time and cross-browser testing.
But, as others have already said, you can't write server files without server code.

javascript vs Java EE / PHP / other web development apps

I am wondering in what situations would it be better to use javascript instead of Java EE/PHP/others? Because before I only know javascript, then when I learned Java EE/PHP, I never use javascript anymore because I do everything in Java EE or PHP.
I hope you can enlighten me.
Javascript tends to be on the client (browser), even though some is starting to appear on the server side (node.js, for instance).
The others (PHP, Java EE, ASP.NET, JSP and more) are server side, mostly used for heavy processing, accessing databases etc... They generate HTML (and sometimes javascript) that is sent to the browser.
JavaScript and stuff like PHP is something completely different. JavaScript is clientside whereas PHP is sererside. Clientside code is executed by the site's viewer while serverside code is executed on the server and sends the results to the client. Serverside is also suited for accessing databases and other stored data.
You should use serverside code to generate your webpage to show the user, and clientside code such as JavaScript to give the users a more interactive experience.

Write a serverside c++/openGL App, that is accessible via JavaScript

I am currently having an idea where I want to save an image from a c++/openGL application on demand from a browser. So basically I would like to run the application itself on the server and have a simple communication layer like this:
JS -> tell application to do calculations (and maybe pass a string or some simple data)
application -> tell JS when finished and maybe send a link, text or something as simple as that.
I don't really have alot of experience with webservers and as such don't know if that is possible at all (it's just my naive thinking). And note: I am not talking about a webGL application, I just want to have simple communication between a c++ serverside application, and the user.
Any ideas how to do that?
Thanks alot!
Basically no matter what your language/framework you choose for your web server, you just need a interface that is callable from your browser JS, and you can do whatever you want in the server once it recieves the call.
Most likely any web service interface exposed from the server.
Just need to safeguard your server not to get DoS since it sounds like it's a huge process.
As far as I know, JavaScript (at least when embedded in HTML) is executed on your local machine and not on the server so that there is IMHO no way to directly start your server-application using JS.
PHP for example is executed on the server-side and so you could use e.g. the php system function to call your C++/OpenGL application on the server - initiated on demand through a web-browser.
When the call is finished you could then directly present the image.
Well you could always use the cgi interface to invoke your application
and have it save that image somewhere accessible to the webserver.
Then have your js load that via ajax.
Or make a cgi App that talks to the app and then serves a small
page with the pic in it.
[EDIT]
Answering the comments:
CGI is not complex to learn, it is mostly a simple convention
you can follow. I think it would give you the maximum of
flexibility. I don't know which php mods allow you to leave the cozy protection of the server-application and interact with other stuff on your server.

Categories

Resources