struggling to get cookie information to print in PHP - php

For some reason, I can't get the value of "price" from a cookie.
Here's the cookie info.
[{"label":"300 HOME ","price":150},{"label":"Yes, my phone line is (1234567890)","price":40},{"label":"internet 2","price":0},{"price":-25}]
And here's the code I'm using. What am I doing wrong?
<?php
$cookie = $_COOKIE["MonthlyOptions"];
$obj = json_decode($cookie);
print $obj->{'price'};
?>

Thanks to both #waterloomatt and #Ravi Patel for their suggestions. In the end, what was stopping me from progressing was the need to add stripslashes.
$cookie = (stripslashes( $_COOKIE["MonthlyOptions"] ));
$obj = json_decode($cookie);
print $obj[0]->price;

Related

session data won't show

I don't know the problem.
The data is saved in the session but it only shows the last input.
$_SESSION['data'][] = $_POST;
$_SESSION['data']['lengtezijde'] = $_POST['lengtezijde'];
$_SESSION['data']['kleur'] = $_POST['kleur'];
$_SESSION['data']['hoogte'] = $_POST['hoogte'];
?><tr><?
?><th><?echo $_SESSION['data']['lengtezijde'];?></th><?
?><th><?echo $_SESSION['data']['kleur'];?></th><?
?><th><?echo $_SESSION['data']['hoogte'];?></th><?
?></tr><?
I have tried your code also and it work me. Since it is an array, you must loop trough it to display the values and that is why I think it is giving you only the last value.
//Declare your variables
$lengtezijde = $_POST['lengtezijde'];
$kleur = $_POST['kleur'];
$hoogte = $_POST['hoogte'];
//Store it in session
$_SESSION['data'] = array(
'lengtezijde' => $lengtezijde,
'kleur' => $kleur,
'hoogte' => $hoogte,
);
Now you can loop through your data and display it. I hope this helps.
if i understand what you looking for you must check your $_POST
i test this code
$_SESSION['data'][] = "Test";
$_SESSION['data']['lengtezijde'] = 'test1';
$_SESSION['data']['kleur'] = 'test2';
$_SESSION['data']['hoogte'] = 'test3';
echo $_SESSION['data']['hoogte'];
br();
echo $_SESSION['data']['kleur'];
br();
echo $_SESSION['data']['lengtezijde'];
result :
test2
test1
test3
Make sure you have mentioned session_start() in First Line of your code ,
session_start() too, before you assign values to $_SESSION
Rest of things seems ok in your code.
Never use below kind of assignment it will ruin your machine memory . If you run your script you will see that on each refresh of the page this size will be increasing. that is not advisable.
$_SESSION['data'][] = $_POST;

Cannot Parse Paypal "notify_url" Data Into an Associative Array (PHP)

I am setting up a Paypal sandbox. Everything works fine. When the transaction is done, paypal POSTS some data to my "notify_url" page (which I've named process-payment.php).
Now, when I post:
$array = $_POST;
$encodedString = json_decode($array);
Now, I can PUT that encoded string in the database, and it looks like:
{"mc_gross":"10.00","protection_eligibility":"Eligible",
"address_status":"confirmed","payer_id"}
Now, my big question is, how can I get THAT (^^^) into an associative array, where I can store those values in a database that records the transaction? Thank you so much for your help in advance! I've already tried:
$pp_array = file_get_contents('php://input');
$arrayDump = json_encode($pp_array);
$pp_array = json_decode($pp_array, true);
Which, obviously, didn't work. So, kinda hoping someone can give me a little tutelage here!
You are trying to use json_decode on an bad array because your payer_id is NULL. Consider filling or removing it. This is a working example with a filled payer_id:
$json = '{"mc_gross":"10.00","protection_eligibility":"Eligible", "address_status":"confirmed","payer_id":"2"}';
$json_asoc = (json_decode($json, true));
print $json_asoc['mc_gross']; // 10
For details see here
So, turns out that the code I used to make it work was this:
$array = $_POST;
$arrayDump = json_encode($array);
file_put_contents('payment-record.txt', $arrayDump);
$fileContents = file_get_contents('payment-record.txt');
$pp_array = json_decode($fileContents, true);
That gave me a workable array. Not sure why I had to write it first, but there you go.

PHP trying to use str_replace()

This is my current code:
$parcels = $api->parcels->get();
$url = (array_values($parcels)[0]['label']['label_printer']);
$goToUrl = $api->getUrl($url);
str_replace('/api/v2//api/v2/', '/api/v2/', $goToUrl);
print_r($goToUrl);
echo "<br />";
echo $url;
Why do I use str_replace()? because I am intending to redirect to $goToUrl and this is not working because the current API is giving me the link wrong.
This is my output:
https://api_key:api_secret#panel.sendcloud.nl/api/v2//api/v2/labels/label_printer/1369315
As you can see, api/v2 comes in this link twice. I want to remove /api/v2/ and then run the output. But my str_replace(); is not performing. My output stays the same.
Can this even be achieved this way? Thanks for any help in advance.
Try changing the str_replace line to:
$goToUrl = str_replace('/api/v2//api/v2/', '/api/v2/', $goToUrl);

Steam url api inserting variables

I have been attempting to use steam api to display a persons name. When I insert there steamid in directly it works however, when I try to use a variable it doesn't work. The thing that confuses me is that I have this exact same code for my queue that works however this does not work. I have looked around and I believe I am doing this right but for some reason it is not working. Any help here would be great. Thanks.
<?
$name = 76xxxxxxxxxxx;
$response = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/? key=xxx&steamids={$name}&format=json";
$middle = (file_get_contents($response));
$json = json_decode($middle, true);
$b = $json['response']['players'][0]['personaname'];
print_r($b);
?>
Your $name variable needs to be wrapped in quotes.
$name = "76xxxxxxxxxxx";
This will properly create your response variable

Using Simple HTML DOM to extract an 'a' URL

I have this code for scraping team names from a table
$url = 'http://fantasy.premierleague.com/my-leagues/303/standings/';
$html = #file_get_html($url);
//Cut out the table
$FullTable = $html->find('table[class=ismStandingsTable]',0);
//get the text from the 3rd cell in the row
$teamname = $FullTable->find('td',2)->innertext;
echo $teamname;
This much works.. and gives this output....
Why Always Me?
But when I add these lines..
$teamdetails = $teamname->find('a')->href;
echo $teamdetails;
I get completely blank output.
Any idea why? I am trying to get the /entry/110291/event-history/33/ as one variable, and the Why Always Me? as another.
Instead do this:
$tdhtml = DOMDocument::loadHTML($teamdetails);
$link = $tdhtml->getElementsByTagName('a');
$url = $link->item(0)->attributes->getNamedItem('href')->nodeValue;
$teamdetails = $teamname->find('a')->href;
^^^^^^^^^---- never defined in your code
I also fail to see how your "works" code could possibly work. You don't define $teamname in there either, so all you'd never get is the output of a null/undefined variable, which is...no output all.
Marc B is right, I get that you don't have to initialize a variable, but he is saying you are trying to access a property of said variable:
$teamdetails = $teamname->find('a')->href;
^^^^^^^^^---- never defined in your code
This is essentially:
$teamname = null;
$teamname->find('a')->href;
The problem in your example is that $teamname is a string and you're treating it like a simple_html_dom_node

Categories