Edit a .txt file with javascript [duplicate] - javascript

This question already has answers here:
Javascript - create text file on website
(5 answers)
Closed 5 years ago.
I want a user on my website to type in a string. I want this string to be stored in a txt file. When another user types in a string I want this string to be stored in a seperate line in the txt file, so I can read all the data, which was typed in later.
I searched the web for a good solution, because I don't know how to edit a txt file with JavaScript. Unfortionately everything I found is written in jQuery, which I did not learn yet. Does somebody know a simple solution to this problem?

Short answer: you can't on the client, you can on the server.
Bigger question is why? If you want to store user input so they have it later you can use LocalStorage, IndexedDB or even just a cookie to store it.
If you want to save it on the server text files are usually a bad idea - there are a lot of server side storage options that are better than plain text, but you'll need to provide a lot more information for us to help on that.

Related

How to hide sensitive information in Javascript [duplicate]

I am trying to send a json object that holds quiz data [including answers] from my code behind to javascript. I used...
var quizJson = <%=jsonObj%>
but the issue is, my users are smart enough to use view source and reveal the answers. Any suggestion?
Thanks in advance
The only correct solution is not to send the answers to the browser in any form. The browser needs to send the answers that the user enters back to the server. Then you use the server-side code to determine if the answers are correct.
Do an AJAX call when the user chooses an answer. This way, validation will be external and the users won't be able to view the source to find the answer.
Anything you send to the clent for use in client side scripts can be read by any client. Even if you somehow obscure the answer in the source, anyone can pull up a debugger and see the real answers when you "decode" them. This is just like a lot of classic game hacks, where people would disable walls and see what is past them. Anything you send to the client is vulnerable!
The only way to protect the answers is to keep them on the server. Post the attempted answers to the server, then grade them there and return the results to the client.
Use a simple encryption (like this) in order to hide the text from the user. If the data is not important, then that will drive off the average inspection.
However, if the answers are important to keep hidden, then keep them hidden...and off client side.

How does Stackoverflow remember what I entered in the input before I posted? [duplicate]

This question already has answers here:
Persist variables between page loads
(4 answers)
Access user input value saved on one page on another page using javascript
(2 answers)
Closed 3 years ago.
When I start writing a question on Stackoverflow without finishing, I can leave the page, and when I return the input still is keeping what I had entered. How can this be done? Is a database absolutely necessary?
I would like to do something similar for a small webapp. Its a question-app where users answer questions with "No"/"Maybe"/"Yes". Depending on the answer, different points are awarded. When the user quits the test and returns, I would like to be still able to return the results of the last time he used it.
Can this be done completely in JavaScript?
What the test looks like
You can store persistent information in localStorage - which is obviously local on the computer they're using.
Reference: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
If you want this to across many systems/browser, you should use Database and store results and get back when access again.
If this feature restricted to the browser that you logged in, browsers localStorage alone will do the job.
Can this be done completely in JavaScript?
Yes if you are using localstorage.

Reducing Query string size of PHP form [duplicate]

This question already has answers here:
How to compress/decompress a long query string in PHP?
(9 answers)
Closed 8 years ago.
I current have a URL like this
http://blahblah.com/process.php?q=[HUGEEEEEEEEEEEEEEEEEEEEEEEE STRING of 5000 chars]
My goal is to convert this something like
http://blahblah.com/process.php?q=[less charcters]
The first question:
How do I perform a function (encryption function for instance) on my GET variables before it is sent to the action page?
I've seen many questions asked with a similar topic.
The second question:
Assuming, I can do the above by some means (maybe by jQuery/JavaScript or something). How do I compress in the index.php page and decompress in the process.php page?
My attempt:
Searching for functions with fixed lengths:
I've looked at some encryptions that maintain the string size for ex. md5() gives a standard length that is short and tidy even for an extremely huge string. But unfortunately md5 cannot be decoded easily. Is there any other such function that I decode and which has a fixed length? If so, I could use that assuming I know a way to do Step 1.
EDIT: I write a request not to mark as a duplicate of that question and a question which hasn't been answered have specifically been asked again.. Please read #Jeremy 's comments, he was following this post.
I personally think it is best to use POST to send the data to the page. I am pretty much sure you can not use anything like MD5 to 'compress' the data because what MD5 does is hash the data, so it will look at your data run an algorithm to create this fixed length hash.
However, there is an extremely small possibility that two data sets will create the same hash, therefore it seems to me impossible to reliably decrypt MD5 or other similar hashes. Check out this page for more on hash collisions.
Your problem is that you are using the internet the wrong way. The URL is limited (and it depends on the browser), so don't event to try to use long URLs - even when you want to shorten it.
Please keep in mind, that we are using the WordWideWeb for a long time and if you come into a deadend you just have to rethink your problem. Maybe you are using your current technology the wrong way.
So, use POST instead to transfer your data (as others mentioned before).
If you want to "compress" your data you should use a zip like thing and then you must make that URL confirm like BASE64 afterwards. This is not suitable in any way and completly hideous. (And of course it can not guarantee the length of your URL).
MD5 is a hash not a compression thing. MD5 is not reversable. Once you hash something you can not go back again. This is not a magical way to compress tons of megabytes into a single short number. This is to have a short thing that can tell if the original data was modified (if you do that twice).
See http://en.wikipedia.org/wiki/Hash_function
See http://en.wikipedia.org/wiki/MD5
BTW: It is the same as How to compress/decompress a long query string in PHP?

Strange javascript codes... Encrypted, Encoded or Packed? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I obfuscate JavaScript?
I was browsing some sites and found really interesting thing. I am just starter in this coding and never seen such a thing, so I was wondering is it encrypted or encoded or packed or is there anything else?
Script sample:
V10861992380165541086199238016554108619923801655410861992380165541086199238016554108619923801655410861992380165541086199238016554='13047389474143951304738947414395130473894741439513047389474143951304738947414395130473894741439513047389474143951304738947414395130473894741439513047389474143951304738947414395130473894741439513047389474143951304738947414395130473894741439513047389474143951304738947414395130473894741439513047389474143951304738947414395130473894741439513047389474143951304738947414395130473894741439513047389474143951304738947414395130473894741439513047389474143951304738947414395130473894741439513047389474143951304738947414395'
or here is screenshot of one really long thing, couldnt even snap it all over my screen.
http://snpr.cm/8KznHp.png
http://snpr.cm/xOLfRE.png
Can anyone tell me what are these, and how can I do the same?
Do I need to pay for an program or something? Thank you for understanding.
All the line or code does is create a variable starting with V and put the number in it. Without seeing the rest of the code I cant tell if it is just encoded or encrypted as well, but if you notice the string is just repeating the number 1304738947414395.
You can definitely do a simple encoding by your self. A simple encoding is to put all the javascript code in a string like aaa="document.write('blah')" and then say aaa="atob(aaa) which converts the original string to base64. Save the base64 string and then place it in an eval statement like eval(btoa(aaa)) that converts it back to text, and then the eval executes the text. When it's finished you have some encoded mildly obfuscated code.

Differences/Advantages of using JSON over XML or vice versa? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
When to prefer JSON over XML?
i wonder if JSON is the preferred choice for AJAX transfers (if this is what its called) over XML? i see that usually JSON is used in jQuery docs over XML?
JSON is more compact than XML and will consume less bandwidth. It is also easier to manipulate with javascript because you are working directly with objects while XML needs to be parsed.
There are a lot of sites out there with pages discussing the relative merits of the two.
This page for example, has one very good reason why you might want to use JSON over XML:
From the start, JSON is already has a leg up on XML for one reason: it’s faster. To read XML you need to parse it, read the nodes, attributes, and child nodes in the XML document, and then use the data that you’ve found.
However, with all of these questions you need to use the technology that best suits your particular problem rather than assuming that one of them can solve everything.

Categories