PHP unserialize : everything works fine except one entry - php

I'm trying to unserialize some data stored in mysql.
Everything is okay except ONE field that can't be displayed.
The data :
a:4:{
s:7:"prénom";s:6:"Johnny";
s:3:"nom";s:5:"Rocky";
s:12:"NOT WORKING";s:2:"on";
s:7:"opt in2";s:2:"on";
}
that I'm calling with
<?php $mydata = $result['data'];
$mydata = unserialize($mydata);
echo $mydata['prénom'];
echo $mydata['nom'];
echo $mydata['NOT WORKING'];
echo $mydata['opt in2'];
?>
"prénom", "nom" and "opt in2" are correctly displayed, but "NOT WORKING" isn't (nothing is displayed).
What is interesting in the latter is that the number before the string do not match the number of letters, you can see it's written 12 while the string has 11 letters (space included). I tried to change it to 11 but it screws everything.
What is even more fun is that all the values (including "NOT WORKING" can still be properly displayed with something like :
<?php foreach($result['data'] as $fieldName => $fieldValue){?>
<?php echo esc_html(strip_tags(wp_kses_stripslashes(ucwords($fieldName)))); ?>
<?php echo esc_html(strip_tags(wp_kses_stripslashes(ucwords($fieldValue)))); ?>
Can someone explain me what is happening ? I can't see any logic in here.
Thanks a lot !

Related

Show HTML if URL contains a defined domain name, else show something else

I am building an admin panel that can be used on different sites. I need to show some specific HTML if the url contains a specific domain, and something else if not.
So, if the URL is something like this http:// (something) .mydomain.com/admin.... I would like to show some HTML.
If the URL is something like this http:// anotherdomain.com/admin... I would like to show something else.
Í have tried with this, but somehow didn't get that to work. What am I doing wrong?
Edit: Wrong code. See code below...
<?php
if (substr($_SERVER['REQUEST_URI'], 0, 5) !== '/mydomain.com') {
echo '<div id="stuff"></div>';
}
?>
Sorry: Posted the wrong php code. A bit too tired I think...
This is the one I messed with:
<?php
if (substr($_SERVER['SERVER_NAME']) !== 'mydomain.com') {
echo '<div id="stuff">test</div>';
}
?>
Please try below code
<?php
$url_segments = parse_url("http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
if (strpos($url_segments['host'],'mydomain.com') !== false) {
echo 'true';
}else{
echo 'false';
}
?>
Thanks,
Try with that code:
if (preg_match ( string $pattern , string $subject)){}
http://php.net/manual/de/function.preg-match.php
http://php.net/substr
Your current substr is saying "If the first 5 characters of the request URI aren't /mydomain.com, print the stuff below."
You could try to do this correctly in a few ways. You could use strpos and checking to see if mydomain.com appears in the request URI, or a more complicated regex to find something like (mydomain.)
Change $_SERVER['REQUEST_URI'] for $_SERVER['SERVER_NAME'] if mydomain.com it's your domain name.
Also, the substr is returning 5 chars and you are comparing against 13 chars

Php returning array from json, but I need the data as a string

So I'm setting up a little page for myself to see my online transactions in dogecoin.
I have the RPC server/client connection established and working correctly.
The listtransactions method provides me with the transaction history as an array, which breaks down into its elements. I embed these in a table.
That all works correctly. what I WANT is to take the transaction ID and make it linkable to the dogecoin blockchain record.
Here is the code, and then I will note which lines have the issue:
for($i=count($json)-1; $i>=0; $i--){
echo '<tr>';
echo '<td>'.$json[$i]['address'].'</td>'."\n";
//echo '<td>'.$json[$i]['category'].'</td>'."\n";
echo '<td>'.$json[$i]['amount'].'</td>'."\n";
//echo '<td>'.$json[$i]['confirmations'].'</td>'."\n";
echo '<td>'."<a href='https://dogechain.info/tx/$json[$i]['txid']'>".$json[$i]
['txid'].'</a> </td>'."\n";
echo '</tr>';
}
echo '</table></center>';
?>
The line containing the hyperlink is where it screws up. You can see how it is displaying at http://www.suchco.in/
an example link it gives me is: https://dogechain.info/tx/Array%5B
what it should be is: https://dogechain.info/tx/ fab3b949cb3a71e79fa6b631d5d16aa7268b77dc3626e2fb2e711f1b43adc08d
how can I make this happen?
Put { } around your array variables in a string.
Try:
{$json[$i]['txid']}
Instead of:
$json[$i]['txid']
OR
Do string concatenation
echo '<td>'."<a href='https://dogechain.info/tx/".$json[$i]['txid']."'>"

Checking if an element of an array is present in another array

I am writing a php script which aim is to check whether any of the URLs submitted by the user in the text area are present in an array of other URLs. Unfortunately the script does not work as expected and I can't figure out how to correct it. I have the following code:
<?php
$gwt_links = $_POST['gwt_links'];
$gwt_links_exploded = preg_split('/\r\n|\n|\r/', $gwt_links);
$blacklisted = file('blacklist.txt');
foreach ($gwt_links_exploded as $gwt_link) {
if (in_array($gwt_link, $blacklisted)) {
echo 'link found';
}
else {
echo 'link not found';
}
}
?>
If I submit URLs in a text area, the script returns 'link not found' even if the URLs are present in blacklist.txt file. I suppose the problem lies in reading the file into an array - I think some special characters must be added. I tried removing them by trim, however without success... How should I correct the script to make it work?

Returning value from powershell script can not be used with PHP

Well guys i dont really know how to start, is the first time that i work with powershell and PHP together, but now i dont know how to deal with this problem, therefore im here..
Im developing a webinterface for a company to administrate services (turn on/off servers basically). I have already programmed the powershells scripts, everything run fine 0 problems.
But i have a part where i try to show in a AlertBox(javascript) the status of a process (the powershell script has already given me the return value but i cant understand why php dont process it like a string or text.
Check it Out a piece of my code :
<?php
if (isset($_POST['button']))
{
$service = $_POST["service1"];
$psPath = "powershell.exe";
$psDIR = "C:\\xampp\\htdocs\\shell\\";
$psScript = "check-status.ps1";
$runScript = $psDIR. $psScript;
$runcheck = $psPath." ".$runScript." 2>&1";
$computername = "xen-80-42";
$go = shell_exec("$runcheck -passwd 'Hpdprqn5' -user 'Administrator' -computer '$computername' -service '$service' < NUL");
echo "<script type='text/javascript'>alert('" . $service . " is currently " . $go . " on " . $computername . "'); </script>";
}
?>
<form method="post">
<p>
<input type="text" name="service1" id="service1" maxlength="20"></input> <button name="button">Check Status</button>
</p>
</form>
Well
Everything works fine, the script give back 2 possible values : "Running" or "Stopped"
that means , the value of the variable $go, should be or "Running" or "Stopped"
Ok i tried with a process that is already running, therefore : echo $go;
print "Running"
i thought was a problem because maybe the returned value $go is not String.
But i solved that using gettype($go) , and the variable is STRING!
but can somebody explain me why when i wanna count the characters of that String, using strlen($go) , i get a return of 8 characters? RUNNING has only 7 Letters, not 8
for that reason i cant execute the ALERT in Javascript (code above), because java doesnt take it as a String or something like that
something is wrong there i think, btw when i tried to make a comparation like that:
if ($go == "Running") { echo "Everything ok"; } else { echo "different values";}
i get the second options, both values are not really equal, they are diferent. but wheree? i cant see anything..
I hope someone can helpme cause im frustrate :(
btw i tried :
strval($go);
(string)$go;
$var = "$go";
NOTHING WORKS :(
if its necesary ask for the powershell script, but i think its not necesary you see that php receives the value as STRING :/
I guess the mysterious symbol is a regular newline character. Try using trim() instead;

PHP echo not showing up in one computer?

I have a php script that queries a MySQL database and populates a drop-down menu using the data received. Everything was working fine and suddenly, the echo "Custom" option doesn't show up for me. I asked someone else to check the same page, and it showed up for him. I tried changing browsers, and nothing. Does anyone know why this would happen?
echo '<div class="c_element" style="height: auto;">
<select class="c_sel">';
$c= mysql_query("SELECT * FROM C WHERE c_lo_id =".$sel_lo_id) or die(mysql_error());
while($row = mysql_fetch_array($c))
{
echo '<option value='.$row['c_id'].'>'.$row['c_name'].'</option>';
}
echo '<option value="0">Custom </option>
</select>';
This can happen if your values contain characters that break your html, like ', > or <. When outputting to html, you should always make sure that these are encoded correctly.
Apart from that this would also happen if there are spaces in your values as you don't quote the attribute value.
With both corrections:;
echo '<option value="'.htmlspecialchars($row['c_id']).'">'.htmlspecialchars($row['c_name']).'</option>';
^ added as well ^

Categories