I am trying to use base64_encode to display the ids differently. I am also trying to add a large number to the id so the string will look longer.
The problem is I encrypt a number to make it a string. Then when I decrypt the same string I expect the same value but in my case it is returning different values.
Why I am not seeing the same values?
This is my code:
define('IDS_SALT', 852045641596357);
function simple_encode($id){
$data = '';
$id += IDS_SALT;
$data = base64_encode($id);
$data = str_replace(array('+','/','='),array('-','_','.'),$data);
return $data;
}
function simple_decode($code){
$data = '';
$code = str_replace(array('-','_','.'),array('+','/','='),$code);
$id = base64_decode($code);
$id -= IDS_SALT;
return $id;
}
//this is returning "OC41MjA0NTY0MTU5NjM2RSsxNA.."
echo 'Encrypted: ';
echo simple_encode(7);
echo '<br />';
//this is returning 3 a NOT 7. it should return 7
echo 'Encrypted: ';
echo simple_decode( simple_encode(7) );
echo '<br />';
I just ran the same code (exactly as above) on my test server here - I received different results:
Encrypted: ODUyMDQ1NjQxNTk2MzY0
Encrypted: 7
Which looks to be correct?
Is there any other code in your test script that could be interfering?
Steve
Related
Here is my PHP function that grabs data from inputs on the page.
function getCounts($selection, $srch) {
$loadjson = file_get_contents('data.json');
$jsondata = json_decode($loadjson);
$total = 0;
foreach ($jsondata as $list) {
if ($list->$selection == $srch) {
$total .= $list->TOTAL
}
}
echo 'Total - ' . $total . '<br>' . '<br>';
}
I know the issue is with the line "$total .= $list->TOTAL" but I cannot figure out how to take the value that "$list->TOTAL" gets and add it to an integer. I have tested and if I do "echo $list->TOTAL . ','" instead of "$total .= $list->TOTAL" the function spits out a list of all the numbers that are in the JSON data I simply cannot figure out how to get the numbers into an integr and added into one number.
Can someone point me in the right direction?
I need server to return complete IDs that contain an integer(single digit or more) sent in by a user to find a match.
e.g I submit '5678', then server returns 12567891, 54356782, 90567811.
Emphasis on '5678'. My code returns no results.
<?php
$per = array(456755643, 78567561, 443321, 33267554560, 44321122, 554367533, 3390);
$thr = 675;
if(isset($_POST['search'])){
$jake = preg_grep(("/$thr+/", $per);
foreach($jake as $value){
print $value . br;
}
}
?>
try this
$per = array(456755643, 78567561, 443321, 33267554560, 44321122, 554367533, 3390);
$thr = 675;
$jake = preg_grep("/$thr/", $per);
foreach($jake as $value){
print $value . '<br>';
}
//result
456755643
78567561
33267554560
554367533
First and foremost, forgive me if my language is off - I'm still learning how to both speak and write in programming languages. How I can retrieve an entire object from an array in PHP when that array has several key, value pairs?
<?php
$quotes = array();
$quotes[0] = array(
"quote" => "This is a great quote",
"attribution" => "Benjamin Franklin"
);
$quotes[1] = array(
"quote" => "This here is a really good quote",
"attribution" => "Theodore Roosevelt"
);
function get_random_quote($quote_id, $quote) {
$output = "";
$output = '<h1>' . $quote["quote"] . '.</h1>';
$output .= '<p>' . $quote["attribution"] . '</p>';
return $output;
} ?>
<?php
foreach($quotes as $quote_id => $quote) {
echo get_random_quote($quote_id, $quote);
} ?>
Using array_rand and var_dump I'm able to view the item in the browser in raw form, but I'm unable to actually figure out how to get each element to display in HTML.
$quote = $quotes;
$random_quote = array_rand($quote);
var_dump($quote[$random_quote]);
Thanks in advance for any help!
No need for that hefty function
$random=$quotes[array_rand($quotes)];
echo $random["quote"];
echo $random["attribution"];
Also, this is useless
<?php
foreach($quotes as $quote_id => $quote) {
echo get_random_quote($quote_id, $quote);
} ?>
If you have to run a loop over all the elements then why randomize hem in the first place? This is circular. You should just run the loop as many number of times as the quotes you need in output. If you however just need all the quotes but in a random order then that can simply be done in one line.
shuffle($quotes); // this will randomize your quotes order for loop
foreach($quotes as $qoute)
{
echo $quote["quote"];
echo $quote["attribution"];
}
This will also make sure that your quotes are not repeated, whereas your own solution and the other suggestions will still repeat your quotes randomly for any reasonably sized array of quotes.
A simpler version of your function would be
function get_random_quote(&$quotes)
{
$quote=$quotes[array_rand($quotes)];
return <<<HTML
<h1>{$quote["quote"]}</h1>
<p>{$quote["attribution"]}</p>
HTML;
}
function should be like this
function get_random_quote($quote_id, $quote) {
$m = 0;
$n = sizeof($quote)-1;
$i= rand($m, $n);
$output = "";
$output = '<h1>' . $quote[$i]["quote"] . '.</h1>';
$output .= '<p>' . $quote[$i]["attribution"] . '</p>';
return $output;
}
However you are not using your first parameter-$quote_id in the function. you can remove it. and call function with single parameter that is array $quote
Why don't you try this:
$quote = $quotes;
$random_quote = array_rand($quote);
$random = $quote[$random_quote];
echo '<h1>' . $random["quote"] . '.</h1><br>';
echo '<p>' . $random["attribution"] . '</p>';
Want to create a function:
echo get_random_quote($quotes);
function get_random_quote($quotes) {
$quote = $quotes;
$random_quote = array_rand($quote);
$random = $quote[$random_quote];
return '<h1>' . $random["quote"] . '.</h1><br>'.'<p>' . $random["attribution"] . '</p>';
}
First, you dont need the $quote_id in get_random_quote(), should be like this:
function get_random_quote($quote) {
$output = "";
$output = '<h1>' . $quote["quote"] . '.</h1>';
$output .= '<p>' . $quote["attribution"] . '</p>';
return $output;
}
And I cant see anything random that the function is doing. You are just iterating through the array:
foreach($quotes as $quote_id => $quote) {
echo get_random_quote( $quote);
}
According to http://php.net/manual/en/function.array-rand.php:
array_rand() Picks one or more random entries out of an array, and
returns the key (or keys) of the random entries.
So I guess $quote[$random_quote] should return your element, you can use it like:
$random_quote = array_rand($quotes);
echo get_random_quote($quote[$random_quote]);
Now, I use strstr to get data from external JSON file. I'm not sure if it's the fastest way to do that what I want and I can't test because json_decode don't work in my code.
$before = '"THIS":"';
$after = '","date"';
$data = strstr(substr($url, strpos($url, $before) + strlen($before)), $after, true)
and with json_decode:
$address = file_get_contents('http://json.link/?something=Hello');
$data = json_decode($address);
echo $data->data->THIS;
Now, when I replace my first code with second I get:
Notice: Trying to get property of non-object
All my code:
$text = "a lot of text";
$text_split = array(0 => '');
$number = 0;
$words = explode(' ', $text);
foreach($words as $word)
{
if(strlen($text_split[$number]) + strlen($word) + 1 > 500)
{
++$number;
$text_split[$number] = '';
}
$text_split[$number] .= $word.' ';
}
foreach($text_split as $texts)
{
$text_encode = rawurlencode($texts);
$address = file_get_contents('http://json.link/?something='.$text_encode);
$data = json_decode($address);
echo $data->data->THIS;
}
What should in do in that case? Keep using strstr or replace all code to work with json_decode (maybe because execution time is faster?)? If the second option, how I can make json_decode work here? Thanks!
... and sorry for bad english.
LE:
If I replace $address = file_get_contents('http://json.link/?something='.$text_encode); with $address = file_get_contents('http://json.link/?something=Hello'); I get VALID result for "Hello" text but 10 times. I guess because it's in a foreach.
json_decode is the suggested method to work with JSON data. Here I think you are trying to access an invalid property in JSON object.
$data = json_decode($address);
echo $data->data->THIS;
I guess you need $data->date instead of $data-data?
you have to access the specific key value like this
$json = '{"success":true,"msg":"success","data":{"THIS":"thing I need","date":"24.03.2014","https":false}}';
$d=json_decode($json,true);
echo $d['data']['THIS'];
Here is the code in my class file. I have separate class file and a product edit parts with fields. When I access them in new page I get nothing.
function editProd($editid){
include('config.php');
$query= sprintf("select * From product_data where id=".$editid."");
$result=mysqli_query($con,$query);
$data=array();
while($row=mysqli_fetch_array($result) ){
$data[]=$row;
}
return $data;
}
This is how I access it in new page
include('prductclass.php');
$addnewprod=new Productdata();
$addnewprod->editProd($_REQUEST['edit_id']);
but I get nothing in result so how do I fill my form in edit from
var_dump($addnewprod->editProd($_REQUEST['edit_id'])); should get you some output (if the sql query returned results).
If you need it in a variable:
$var = $addnewprod->editProd($_REQUEST['edit_id']);
Or:
$data = $addnewprod->editProd($_REQUEST['edit_id']);
foreach ($data as $row ) {
echo 'Column 1: ' . $row['some_column'name'] . '<br />';
echo 'Column 2: ' . $row['some_other_column'name'] . '<br /><br />';
}
You return $data, so you will have to do something with that data.
Your current code requests the data by using the code $addnewprod->editProd($_REQUEST['edit_id']); but does nothing with the data it receives.
you should get used to a better, consistent style
$myVar = 'foobar';
instead of
$myVar ='foobar';
$myVar= 'foobar';
$myVar='foobar';