String to Integer convertion - identical in PHP and Javascript - php

I have a URL dynamically displayed with a PHP script. This URL comes to be the name of a CSS class. I need to use this class name into a jQuery script after an Ajax call response (All the HTML into this specific class has to be hidden).
The URL contains / and . and : — To make it easier in my jQuery script, I would like to convert the URL into an Integer with a PHP function (like hash("md5",)) ... and in my JavaScript, convert the URL again into an integer that will be obviously the same.
I read that How to calculate md5 hash of a file using javascript
but it doesn't look like the best solution. Does anyone have a more intelligent solution?
Regards

You are probably looking for encoding and not hashing, as you want to read the URL back. I'd try using base64 - on the server side: http://php.net/manual/en/function.base64-decode.php and http://php.net/manual/en/function.base64-encode.php
And on client (JavaScript) side: http://www.webtoolkit.info/javascript-base64.html, How can you encode a string to Base64 in JavaScript?

Related

Does inline css affect the value from a form?

I have input in html like this.
<input name="hoteltaxi" type="text" style="text-transform: uppercase;">
These inputs send the information throughout a post method. The PHP file that receive the information, got to save it in a mysql database.
To this point, is ok. Everything is working, but i have a little question about the form style.
If css can transform the value presentation... can also transform the value that i send to php? ¿Or is just presentation? Because, i really need to see this in uppercase, but i need to store with PHP in the original string format (can also be numbers, lowercases, and uppercases, doesn't matter) because from the original string, previously stored, will use it to make a MD5 hash.
The code is my frenemy.
The answer is No
You can see it clearly in this screenshot
If you really want the data to be transformed to uppercase do it on server side like this
$hoteltaxi = strtoupper($_POST['hoteltaxi']);
more about strtoupper
Or if you want it to be done on client-side a little bit of javascript can help you
$('input[name="hoteltaxi"]').change(function(){
var self = $(this);
self.val(self.val().toUpperCase())
})

How to get whole URL string(including &,?) in PHP?

I want to get only URL string in parameter.
So I coded below :
//In my HTML file.
<script>
document.write('<iframe id="ifr1"
src="http://{domain}/prj.php?url=https://www.google.co.kr/search?sugexp=chrome,mod=3&sourceid=chrome&ie=UTF-8&q=url+utf8+encoding#sclient=psy-ab&hl=ko&newwindow=1&prmdo=1&tbm=klg&q=google&oq=google&aq=f&aqi=g10&aql=&gs_l=serp.3..0l10.2207859.2208404.3.2208459.6.4.0.1.1.3.496.1353.2-2j1j1.4.0...0.0.O1lbexylHQM&pbx=1&prmdo=1&bav=on.2,or.r_gc.r_pw.,cf.osb&fp=b8fee9fdb4e7f90&biw=1440&bih=828&a=' +'asdf'+" width="100%" height="100%" frameborder="0"> </iframe>');
</script>
//In my php file
$message = $_GET['url'];
echo("$message");
But I only get 'https://www.google.co.kr/search?sugexp=chrome,mod=3'.
What's wrong?
I want to get whole URL string. Including &,?,or everything else.
You need to use encodeURIComponent to encode the url parameter.
The reason is that you use an encoding into another encoding.
The first is:
prj.php?url=[somedata]
where [somedata] is an URL with another key/value collection:
https://url/etc/search?key1=value1&key2=value2
If the url parameter is not urlencoded there is no way to distinguish the parameters of /prj.php from the parameters of /search, it is assumed that all the parameters defined with &key=value&key=value... syntax belong all to the first page.
The trick is to url-encode the url parameter, but how you do it depends on the context.
If the url is dinamically generated via PHP, use the urlencode() function.
If your url is hard-coded (not generated via PHP), you can write for yourself a little PHP tool to do the job, or use an online encoder like http://www.opinionatedgeek.com/dotnet/tools/urlencode/Encode.aspx

Reverse obfuscating plain text string urls in PHP

I'm trying to figure out a way to program a function that will de-obfuscate a plain text url.
Something like this:
<input type="hidden" value="kjgajkwe##jktGAkjgWjkajskd" name="obsfucatedString" />
Then in the processing of that form I want to De-Obsfucate it:
$url = deObfuscate($_POST['obsfucatedString']);
so $url would become something like:
$url = 'http://domain.com/filename.zip';
Is something like that even possible?
I'm trying to hide the url from plain programmer sight.
I guess I would need to write something that would obsfucate the string as well
so
$obsfucatedStringURL = obsfucate('http://domain.com/filename.zip');
Encrypt the URL with a password stored on the server (a good algorithm to use is AES), then decrypt it when you need to obtain the value. A problem with this is that the encrypted string will not be composed of printable characters. To get around this, use base64_encode() to convert the binary encoded string to printable characters that can be added as a value in the <input> field, then use base64_decode() to get back the original value on the server.
There are many ways of encoding and reversing a plain text string. An simple way to obfuscate your string is by using the str_rot13 function once to encode and once again to decode (note: this will not give you any cryptographic security). I'd suggest encrypting using AES using a secret stored on the server to encrypt and decrypt. The following thread's answer defines functions for encrypting/decrypting that you can use.
PHP AES encrypt / decrypt
Another approach that might be worth considering vs. obfuscation is to store the URL server side as part of the user's session or persisted in a database. Then instead of sending an obfuscated string down, use a key that performs a lookup to retrieve the URL.

Can PHP load a encrypted file?

For example, I would like to:
require('encrypted.php')
"encrypted.php" contains php code that was encrypted with base64, and I have the key to decrypt it. Is there a way to first decrypt the file and then just load the decrypted code into PHP?
The first way that jumps to mind is:
eval("?".">".base64_decode(file_get_contents("encrypted.php"))."<"."?php");
However that's probably not a good idea (evil eval).
Why have you encrypted a PHP file anyway? Nobody can see the source code unless you mess around with things.
You would need to load the file's contents with file_get_contents() or similar, then call base64_decode() on it and eval() it as PHP. Only do this if you are certain of the contents of the file and you trust its source.
// Only do this is the file is trusted!!!
$phpcode = base64_decode(file_get_contents("trusted_encoded_file.php"));
eval($phpcode);
Note: See #Kolink's implementation if the encoded file contains <?php ?> open/close tags.
you don't need any key to decode base64 data , use base64_decode().
It is not professional solution, but take look at this : http://www.zend.com/en/products/guard/

Pass encrypted querystring between php and asp.net (c#)

I need to pass a single variable in a querystring from one application (in PHP) to another (in ASP.NET). It's a one way transfer...That is I need to encrypt it in PHP and decrypt it in ASP.NET (c#).
I'm barely a newbie on PHP and I'd like not to have to do more than add a tag to the page that needs to do the passing.
The data will be anywhere from 5 - 15 characters..only letters and numbers.
Thanks!
How about something like this?
<?
$query_string = base64_encode($_SERVER["QUERY_STRING"]);
header("location: http://www.domain.com/page.aspx?query_string=$query_string");
//rest of php code...
?>
Then in page.aspx you decode that query string

Categories