$subprompt = serialize($errors);
header('Location: landingpage.php?success=false&prompt=' . $prompt . '&subprompt=' . $subprompt);
then
if(isset($_GET['subprompt'])){
$subprompt = $_GET['subprompt'];
$subprompt = unserialize($subprompt);
print_r($subprompt);
}
I get all the data when I echo just the $_GET variable, however when I try to unserialize it I get nothing; a blank variable.
$subprompt = urlencode(serialize($errors));
urlencode is intended for this purpose.
$url_string = urlencode(serialize($errors));
Try this:
Besides urlencode, you can try base64 encoding the data. A bit "prettier" url :)
$subprompt = base64_encode(serialize($errors));
header('Location: landingpage.php?success=false&prompt=' . $prompt . '&subprompt=' . $subprompt);
And
if(isset($_GET['subprompt'])){
$subprompt = $_GET['subprompt'];
$subprompt = unserialize(base64_decode($subprompt));
print_r($subprompt);
}
To avoid encoding issues, you should send the value as base64_encode(serialize($subprompt)); instead. The result will be about a 33% longer string, so keep in mind to not exceed the maximum url length.
Related
Ok, so right now I've got this URL, which works perfectly:
$html_string = file_get_contents('https://www.123.com/' . $_GET["ticket"]);
What I want to do is to insert $_GET between the URL.
Here's what I mean:
('https://www.123.com/' . $_GET["ticket"] /url-continues-here);
I have tried everything, but can't find any solution how to do it without an error.
Concatenate the 2 strings first and after that use file_get_contents()
$html_string = 'https://www.123.com/' . $_GET["ticket"] . '/url-continues-here';
file_get_content($html_string);
I am trying to redirect a page to:
product_page.php?rest_id=$rest_id&area=$rest_city
for example:
product_page.php?rest_id=3&area=Enfield
At the moment the page is redirecting to:
http://localhost/PhpProject2/product_page.php?rest_id=&area=
Even though i have specified :
if (isset($_GET['rest_id'])){
$rest_id = mysqli_real_escape_string($dbc,$_GET['rest_id']);
}
if (isset($_GET['area'])){
$area = mysqli_real_escape_string($dbc,$_GET['area']);
}
if (isset($_GET['add_item'])) {
(int)$_GET['add_item']]){
$_SESSION['Shopping_cart_' . (int) $_GET['add_item']]+='1';
//redirect
echo"<script>window.open('product_page.php?rest_id=$rest_id&area=$rest_city','_self')</script>"; //header does not work
}
How would i go about getting the echo to re-direct to the correct page.
What i have tried:
Calling both add_item, rest_id and rest_city in the same if asset.
Moving the $_GET rest_id and rest_city closing tag after the echo.
The two above options just stop the page from re-directing all together, this is my first time doing this, any suggestions or tips would be greatly appreciated.
You should use double qotes ["string $variable string"] for variable to work inside a string or if not you should concatenate the string with [.] sign.
echo"<script>window.open('product_page.php?rest_id=$rest_id&area=$rest_city','_self')</script>";
This will not work because of single qoute [']
Regards
There are many syntax errors in your code. Also, the way you echo the string is wrong. You need to concat the variables with the string, using the PHP concatenation operator, else use double quotes.
Below fixed your errors:
if (isset($_GET['rest_id'])){
$rest_id = mysqli_real_escape_string($dbc,$_GET['rest_id']);
}
if (isset($_GET['area'])){
$area = mysqli_real_escape_string($dbc,$_GET['area']);
}
if (isset($_GET['add_item'])) {
$_SESSION['Shopping_cart_'] = (int) $_GET['add_item']+='1';
echo"<script>window.open('product_page.php?rest_id=" . $rest_id . "&area=" . $rest_city . "','_self')</script>";
}
let me start by saying that I have no idea how to formulate this question, I have spend the last two days looking for some ways to to the following.
I send some information encoded using base64 as follow....
Values are:
Lóms Gruñes
this values came from an input box
beacuse of that I do this
$name = htmlentities(( $_POST ['name'] ) , ENT_NOQUOTES, 'UTF-8');
$midn = htmlentities(( $_POST ['midn'] ) , ENT_NOQUOTES, 'UTF-8');
the output for this should be
Lóms Gruñes
And that is what gets encode, until that everything is fine, and I can do whatever I need with that, in this case I'm going to encode it using base64
$datas ='&name='. $name .'&midn='. $midn;
$bd = base64_encode($datas);
// Now lets send that info to another file..
header( 'Location: other.php?d=$db' ) ;
So the url will be something like
domain.com/other.php?d=TCZvYWN1dGU7bXMgR3J1Jm50aWxkZTtlcw==
So now lets decode it so that it can be saved...
$ds = base64_decode($_GET['d']);
parse_str($ds, $params);
$name = htmlentities($params['name'], ENT_NOQUOTES);
$midn = htmlentities($params['midn'], ENT_NOQUOTES);
It looks pretty straight forward isn't it... but here is the problem because when I try to use the values nothing happen... lets say I just want to echo it...
echo $name . '<br>';
echo $midn;
What I get is
L
Gru
so where is the ó and the ñ?
ok, let say I don't encode anything so the URL will look like this...
domain.com/other.php?name=Lóms&midn=Gruñes
// and the I use echo like this:
echo $_GET['name'] . '<br>';
echo $_GET['midn'];
// the output is
L
Gru
Even if I put : header ('Content-type: text/html; charset=utf-8'); after the <?php ... nothing happen... so, the question... how can I get the ó as a value or better yet, how can I send the í,ó,ñ,á...etc as is in the url domain.com/file.php?data=íÄÑó and retrive it as is and save it as is and display it as is...
I;m not sure if this has some relevant information, the data is going to be saved in a DB, the DB is InnoDB, utf8_general_ci
Thank you for taking the time...
Always escape/encode for the technology you're using.
To get the UTF-8 encoded values from the form submission:
$name = $_POST['name'];
$midn = $_POST['midn'];
To output those into HTML:
<p><?php echo htmlspecialchars($name, ENT_COMPAT, 'UTF-8'); ?></p>
To embed them in a URL:
$url = sprintf('foo.php?name=%s&midn=%s', rawurlencode($name), rawurlencode($midn));
// or
$url = 'foo.php?' . http_build_query(array('name' => $name, 'midn' => $midn));
In foo.php, to get the values back:
$name = $_GET['name'];
$midn = $_GET['midn'];
And again, to output those into HTML:
<p><?php echo htmlspecialchars($name, ENT_COMPAT, 'UTF-8'); ?></p>
And that's basically all you need to do. Always escape values using the right function for the medium, don't escape more or earlier than you need to.
First of all, if you want to decode htmlentities try html_entity_decode().
For encoding the url try urlencode().
Example from php.net:
<?php
$query_string = 'foo=' . urlencode($foo) . '&bar=' . urlencode($bar);
echo '<a href="mycgi?' . htmlentities($query_string) . '">';
?>
Hope it helps!
May be this will work for you . Instead of using htmlentites I used rawurlencode and rawurldecode function .
Below is same code
First get the values from the
$name = rawurlencode('Lóms Gruñes'); use can use //( $_POST ['name']
$mid = rawurlencode('Gruñes'); and here ($_POST ['midn'] )
then
echo $datas ='name='. $name .'midn='. $midn;
$en = base64_encode($datas);
Append this on next url . Then get the value and decode it using base64_decode function
$dd = base64_decode($en);
parse_str($dd);
echo $name;
echo $midn;
I am trying to decode some JSON into a php array. Here's the code excerpt:
$getfile="{"fname":"bob","lname":"thomas","cascade":"bthomas","loc":"res","place":"home 2"}";
$arr = json_decode($getfile, true);
$arr['day3'] = $selecter;
echo(print_r($arr));
The only thing that gets returned is '1'. I've checked JSONLint and it is valid json, so I'm wondering why json_decode is failing. I also tried checking what the array is before adding the day3 key to it, and I still return a '1'. Any help is appreciated!
Actual code:
$getfile = "";
$getfile = file_get_contents($file);
if ($getfile == "") {
writeLog("Error reading file.");
}
writeLog("getfile is " . $getfile);
writeLog("Decoding JSON data");
$arr = json_decode($getfile, true);
writeLog("Decoded raw: " . print_r($arr));
writeLog("Editing raw data. Adding data for day " . $day);
$arr['day3'] = $selecter;
writeLog(print_r($arr));
$newfile = json_enconde($arr);
writeLog($newfile);
if (file_put_contents($file, $newfile)) {
writeLog("Wrote file to " . $file);
echo $newfile;
} else {
writeLog("Error writting file");
}
These are the contents of $file (it's a text file)
{"fname":"Bob","lname":"Thomas","cascade":"bthomas","loc":"res","place":"home 2"}
We still don't know what's in your file. However if:
"{"fname":"bob","lname":"thomas","cascade":"bthomas","loc":"res","place":"home 2"}"
Then the extraneous outer double quotes will screw the JSON, and json_decode will return NULL. Use json_last_error() to find out. Might also be a UTF-8 BOM or something else ...
Anyway, the 1 is the result from print_r. print_r outputs directly, you don't need the echo. Also for debugging rather use var_dump()
More specifically you would want the print_r output returned (instead of the boolean success result 1) and then write that to the log.
So use:
writeLog(print_r($arr, TRUE));
Notice the TRUE parameter.
First, use a single quote. That will cause a parse error
$getfile= '{"fname":"bob","lname":"thomas","cascade":"bthomas","loc":"res","place":"home 2"}';
I assume you have already declared $selecter and it has been assigned to some value.
Remove echo from echo(print_r($arr)) You don't need echo. print_r will also output. If you use echo, it will display 1 at the end of array.
The working code:
$getfile = '{"fname":"bob","lname":"thomas","cascade":"bthomas","loc":"res","place":"home 2"}';
$arr = json_decode($getfile, true);
$selecter = 'foobar';
$arr['day3'] = $selecter;
print_r($arr);
Hope this helps.
I had this problem when doing it like this:
if ($arr = json_decode($getfile, true)) {
$arr['day3'] = $selecter;
echo(print_r($arr));
}
Decoding it outside of if was the solution.
Im trying to achieve an output like this
{"status":"ok","0":{"id":"11","title":"digg","url":"http://www.digg.com"}}
but instead i am getting this
{"status":"ok","0":{"id":"11","title":"digg","url":"http:\/\/www.digg.com"}}
this is the php code im using to generate the json
$links = array('id'=>'11','title'=>'digg','url'=>"http://www.digg.com");
$msg = array('status'=>'ok',$links);
echo json_encode($msg);
any idea what is causing this?
UPDATE
i should have been more clear
if you notice the actual url, its inserting "\" before the "/" in the output. Is this supposed to happen, or is there a way to stop this?
They're both equivalent valid JSON, so it shouldn't matter. The JSON strings:
"http://www.digg.com"
and
"http:\/\/www.digg.com"
both decode to:
"http://www.digg.com"
This is a separate issue, but I would prefer:
$links = array(array('id'=>'11','title'=>'digg','url'=>"http://www.digg.com"));
$msg = array('status'=>'ok', 'links'=>$links);
echo json_encode($msg);
{"status":"ok","links":[{"id":"11","title":"digg","url":"http:\/\/www.digg.com"}]}
This makes more sense to me than having a "0" key, and it extend well if you add more sites:
$links = array(array('id'=>'11','title'=>'digg','url'=>"http://www.digg.com"),
array('id'=>'12','title'=>'reddit','url'=>"http://www.reddit.com"));
$msg = array('status'=>'ok', 'links'=>$links);
echo json_encode($msg);
{"status":"ok","links":[{"id":"11","title":"digg","url":"http:\/\/www.digg.com"},
{"id":"12","title":"reddit","url":"http:\/\/www.reddit.com"}]}
Yes. The JSON specs.