I have used the same code for almost a year and now it stopped working.
I only added one more variable to the string and chrome refuses to accept the cookie on my page, but the same code on Netbeans works.
$cookie_name = "Hoppvader";
$cookie_value = $Vindstyrka .",". $Avstand .",". $Staende . "," . $Markera . "," . $Hojd . "," . $Oversatt . "," . $Vaderbilder . "," . $Translate . "," . $Lufttryck . "," . $color . "," . $LHP_oversatt . "," . $VindBilder;
setcookie($cookie_name, $cookie_value, time() + (86400 * 30 * 12 * 10), "/"); // 10 years
echo '<script type="text/javascript">alert("' . $cookie_value . '"); </script>';
A typical value of the cookie may look like: 1, m/s,1, m,N,Y,m,Y,Y,Y,Y,#FFFFC1,Y,1
And the new value in the string is ",1" or $VindBilder.
The javascript alert prompts the value correct.
I have also tried to roll back the code to where it was before and it still wont save the cookie on my browser.
I also tried it in IE and it saves the cookie, but without the last value ",1".
Anyone ever had this problem?
I found the reason to be that setcookie does not work if do any output prior to setcookie.
http://php.net/manual/en/function.setcookie.php
The problem was not setcookie, it was my mistake to have output before setcookie.
Related
I'm creating player panel for my friend in php, but I have problem with variable shown below. I must insert into variable in php, and send request to database. What is my problem? I can't convert java code, because I don't know how I can do this. It may be strange, but unfortunately it is.
I tried do this with amateur way, using;
require 'mojang-api.class.php';
$uuid = MojangAPI::getUuid('jeb_');
echo 'UUID: <b>' . $uuid . '</b><br>';
echo substr($uuid, 0, 8); echo '-'; echo substr($uuid, 8, 12); echo '-'; substr($uuid, 13, 15); echo '-';
but, You and I know this - this way sucks.
I place the java code below.
Java:
uuid.substring(0, 8) + '-' + uuid.substring(8, 12) + '-' + uuid.substring(12, 16) + '-' + uuid.substring(16, 20) + '-' + uuid.substring(20)
If someone can help me with this problem, I'll be grateful.
Is this what you are looking for
$uuid = MojangAPI::getUuid('jeb_');
echo 'UUID: <b>' . $uuid . '</b><br>';
echo substr($uuid, 0, 8).'-'.substr($uuid, 8, 4).'-'.substr($uuid, 13, 2);
Instead of plus + in Java you use the dot . in PHP. Amazingly you used it in the line above?
I have the following error;
Note: Array to string conversion in [file_path] on line 919
which relates to this line of code where I'm trying to assign this string as a value in an array
$contents[11] = "$hours:$minutes:$seconds\n$ca_1[remaining_time]\n$h:$m:$s";
Why am I getting this error, and how do I resolve it?
It's a bad practice to interpolate string this way because it makes the code very difficult to read, so you should rather use "{$h}" instead of "$h".
As Terminus mentioned in comments, depending on the PHP version,
echo "$ca_1[remaining_time]"
Does not necessarily give a
PHP Notice: Use of undefined constant
Like echo $ca_1[remaining_time] would. But since that didn't work for you, you'd better quote that like ['remaining_time'].
You might also find some interesting things on this topic here.
Second, use curvy braces to explicitly tell what you want to insert:
$contents[11] = "$hours:$minutes:$seconds\n{$ca_1['remaining_time']}\n$h:$m:$s";
This really improves readability.
Try:
$contents[11] = $hours . ':' . $minutes . ':' . $seconds + "\n" . $ca_1['remaining_time'] . "\n " . $h . ':' . $m . ':' . $s";
If this still fails, check your variables. Maybe one of them is an array!?
I'm trying to create a script that displays results based on URL path.
For example, I can have a URL like example.com/A/B/C/D/, example.com/A/B/C/ all the way to example.com/.
A = Year
B = Month
C = Day
D = Title
The script below does a good job of breaking it down.
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$array = explode('/', $path);
The table in the database has a column called 'path'. The path contains '/A/B/C/D/'. Is there an easier way than using multiple if/else statements to accomplish this:
Empty path (home page, limit 5 results:
SELECT * FROM table ORDER BY post_date DESC LIMIT 5;
Search by year:
SELECT * FROM table WHERE path LIKE '/' . $array[1] . '/%';
Search by year and month
SELECT * FROM table WHERE path LIKE '/' . $array[1] . '/' . $array[2] . '/%';
Search by year and month and day
SELECT * FROM table WHERE path LIKE '/' . $array[1] . '/' . $array[2] . '/' . $array[3] . '/%;
Search by year and month and day and title
SELECT * FROM table WHERE path LIKE '/' . $array[1] . '/' . $array[2] . '/' . $array[3] . '/' . $array[4] . '/';
Obviously if the person tries to enter the values (letters) in a different order the results should not be found and an error page be displayed.
Use the array element count to generate your path.
if (count($array)) {
$sql = "SELECT * FROM table WHERE path LIKE '/" . implode($array, '/') . "/%'";
}
Please see SQL Injection
The more cleaner approach would be to define different REST calls for each of the path params. I would use a php micro framework like Flight to handle it. For example if some of the param is not specified then it would be routed to the next and so on. Thus it would be able to handle different params along with the param specific database call. By doing this would eliminate if else logic or any kind of ambiguity.
Look here for reference:
http://flightphp.com/learn#routing
Check the passing mechanism.
I'm trying to split a line of PHP up, the reason being I don't actually always need some of the code and so that I can reuse parts of it.
The main reason I'm doing this is because the currency I get shows more digits for some currencies e.g. 1.2562 instead of 1.25, so I want to use the substr function only on certain GET's and be able to modify it for other GET's.
http://prntscr.com/6ttw8o
symbol is always required, substr isn't, $converter always required, end part of substr isn't however it can change, new currency is required.
$symbol[2] . substr(($converter->convert($defaultCurrency, $newCurrency) * 1), 0, 4) . " <b>" .$newCurrency. "</b>";
I've tried doing this with explode, however I'm not entirely sure how to do it as I have never really had to split anything up before so I'm a little puzzled on how to go about it.
Once the code has gone through the GET checking which is the current one set, I want it to grab the specified split up code pieces and then output it.
Put your code in a function like this:
function formatCurrency($symbol, $converter, $defaultCurrency, $newCurrency, $factor=1.0, $suffix="", $substrChars=0) {
if($substrChars>0) {
return $symbol . substr(($converter->convert($defaultCurrency, $newCurrency) * $factor), 0, $substrChars) . $suffix . " <b>" . $newCurrency. "</b>";
} else {
return $symbol . ($converter->convert($defaultCurrency, $newCurrency) * $factor) . $suffix . " <b>" . $newCurrency. "</b>";
}
}
If you call it without the $substrChars parameter, it will omit the substr() call, otherwise it will strip all but the first $substrChars characters:
if( $_GET['currency'] === "GBP" ){
$newCurrency = $_GET['currency'];
$string = formatCurrency($symbol[1], $converter, $defaultCurrency, $newCurrency, 2.0, ".00", 0);
} elseif( $_GET['currency'] === "USD" ){
$newCurrency = $_GET['currency'];
$string = formatCurrency($symbol[2], $converter, $defaultCurrency, $newCurrency, 1.0, "", 4);
}
This solution is very readable because you immediately see the difference between the two branches in the conditional statement.
I'm using PHP5, MySQL, JavaScript and Fusion Charts.
I have the following time value returned from a database: 2011-12-19 12:00:00
After taking it from the database, I try to pass it through a JavaScript strURL to get it to another page where I can make further database calls using this value. The problem I have is that the JavaScript fails whenever I send it through a date/time. I can send through any other value type and it works so the problem seems to be with the time stamp. I've tried converting it to string before it is passed (just to be sure it's not one already) and that doesn't work. I'm guessing it's to do with the characters within the value. Any idea how to get around this?
The database call in PHP and then sending fields into the JavaScript:
$strQuery = "SELECT unit, watts, time, device, siteid FROM inverter WHERE time = '2011-12-19 12:00:00' AND siteid = '842'";
$result2 = mysql_query($strQuery) or die(mysql_error());
if ($result2) {
while($ors2 = mysql_fetch_array($result2)) {
$thetime = (string)$ors2['time'];
$strXML .= "<set color='58ACFA' label='" . $ors2['device'] . "/" . $ors2['unit'] . "' value='" . $ors2['watts'] . "' link='javaScript:updateChart(" . $ors2['unit'] . " , " . $ors2['device'] . " , " . $ors2['siteid'] . " , " . $thetime . ")'/>";
}
}
And then the JavaScript function:
function updateChart(first, second, third){
//DataURL for the chart
var strURL = "FactoryData.php?factoryId=" + first + "&device=" + second + "&siteid=" + third;
FusionCharts("FactoryDetailed").setXMLUrl(strURL);
}
link='javaScript:updateChart(" . $ors2['unit'] . " , " . $ors2['device'] . " , " . $ors2['siteid'] . " , " . $thetime . ")'
This JavaScript becomes something like
updateChart(341, 454, 842, 2011-12-19 12:00:00);
The first three arguments are valid numbers (I assume the IDs are integers), but the fourth argument causes a syntax error. What you need to do is wrap it in quotes to make it a string:
link='javaScript:updateChart(... " , \"" . $thetime . "\")'
^^ ^^
Now the JavaScript should be like this:
updateChart(341, 454, 842, "2011-12-19 12:00:00");
You can either convert the date to a timestamp with something like
new Date('2011-12-19 12:00:00').getTime()
or encode it using encodeURIComponent('2011-12-19 12:00:00')
The latter is probably the better solution, as it works with all kinds of values, not only dates. You can use decodeURIComponent if you want to read the parameters on the client side, that should already be working fine on the server side.