Please have a look at the following:
foreach($a_Header['Details'] as $i_Detail => &$a_Detail)
{
echo "{$a_Detail['VEH_TREAD_OFF']}\n";
// Make a back-up of the value
$BAK_TREAD_OFF = $a_Detail['VEH_TREAD_OFF'];
// Copy some data from the saved header
foreach(array
(
'POD_QTYORD',
'VEH_TREAD_OFF',
'RPM_SCRM_FIXEDPRICE',
'RPM_TRM_FIXEDPRICE',
'RPM_TRM_COSTPERMM',
'RPM_CTS_CASINGCOST',
'CHARGE_DESC',
'Hide',
) as $s_Column)
{
$a_Header['Details'][$i_Detail][$s_Column] = $a_SavedHeader['Details'][$i_Detail][$s_Column];
}
echo "{$a_Detail['VEH_TREAD_OFF']}\n";
// Now restore our value
$a_Detail['VEH_TREAD_OFF'] = $BAK_TREAD_OFF;
}
You can see that prior to entering the for loop, I have backed up a value which I restore after.
This is because for some reason it is being lost during the loop.
I'm not a PHP guru by any stretch, but this is confusing me no end, as I can't see why modifying one variable would affect another, unless there are wormholes in PHP!
Can someone tell me why this might be happening?
Thanks
$i_Detail => &$a_Detail
Now $a_Detail is a reference to $a_Header['Details'][$i_Detail]. both variables refer to a single value. Do not use & if you do not want that.
Your code sample does not affect $a_detail at all. Are you sure, this is the only reference to this variable? Do you use something like extract()?
Related
I want to assign a value as 0 by default, then every time that page is hit
I want to increase the value by one and when the value is 4 then I have to do some action on that page and change back the value of that variable to 0.
I want to you use global variables or application variables.
I don't want to use the database to save values. I tried my best to save the value in .env but read somewhere it's not the right way to do. Please guide me the best possible way? Working on Laravel 5.3
Thank you very much.
You can create a file in the config folder and store the option there. For example, in a file named system.php:
<?php
return [
"my_option" => 0,
];
Then you can have access to it calling get method from Config:
$myOption = \Config::get('system.my_option');
More info: https://laravel.com/docs/5.2/configuration
You can use session for this:
$counter = (int) session('counter', 0);
session(['error_pages' => ++$counter]);
if ($counter >= 4) {
// here you do what you want
}
Of course in case you want to use this mechanism not in single user session you can use cookies to store counter value.
You can use the config function, is less overhead than use the session:
config(['shared_variable'=>$value])
I'm not a PHP dev and I have little experience with it. I ask for your forgiveness and assistance.
Here's my problem:
I have a script and I need to be able to append a 'key' (I don't know what else to call it) like:
http://my-web-address.com/packages.php?key=secret
When this key is present, I need to run the code responsible for extracting the data from a .json file in a separate directory. (Normally, the code wouldn't index this directory. It should only indexes it when the key is present in the URL.)
I believe this code to be the foreach section in the link above.
I'm having a hard time explaining this in a way that makes sense, so I guess it's easier to show you what I mean.
I know it should be easy; at first I thought I could simply do it with something like this:
if ($key == "secret") {
$document['packages'][] = getPackageData("secretdirectory/secret.json");
}
But alas, simply appending that didn't make it work.
Any ideas?
As Ron Dadon said, but with a slight modification:
sanitize($value) {
// Sanitize the key - see below
return $value;
}
$key = sanitize($_GET['key']);
if ($key == "secret") {
$document['packages'][] = getPackageData("secretdirectory/secret.json");
}
However you should sanitize that input, as anyone can change the key. Here are some resources on that:
Clean & Safe string in PHP
Remove all special characters from a string
The ultimate clean/secure function
You need to use the GET array:
if ($_GET['key'] == "secret") {
$document['packages'][] = getPackageData("secretdirectory/secret.json");
}
I'm trying to make the following form's GET function to be part of a predefined variable.
Any ideas? Thanks in advance!
Let me explain a little more of what I'm really trying to do. I currently run a website concentrating on the U.S. stock market. I've created an HTML form with a method=GET. This form is used like a search box to look up stock ticker symbols. With the GET method, it places the ticker symbol at the end of the URL, and I created a quotes.php page that captures this information and displays a stock chart based on what ticker symbol is keyed into the box. For the company names, I've created a page called company.php that declares all of the variables for the company names (which happens to be a $ followed by the ticker symbol). The file, company.php, is the only file included in quotes.php.
This is where this came in: ' . $$_GET["symbol"] . '
The above code changes the GET into the variable based on what was typed into the form. I've used "die" to display an error message if someone types something into the box that doesn't match a variable in the company.php page.
I've also added into the company.php page variables for each company that will display which stock exchange each stock is listed on. These variables begin with "$ex_". So, what I was trying to do was have the symbol keyed into the box appended to "$ex_" so that it would display the corresponding stock exchange.
My questions are:
Is there a way to have what is typed into the form added to "$ex_"?
Is this an insecure way to code something like this (can it be hacked)?
Thank you all!
Rather than prefixing your variables and using variable variables (that are potentially insecure especially with user input), try this:
$ex = array(
"foo" => "bar",
...
);
if( !isset($ex[$_GET['symbol']])) die("Error: That symbol doesn't exist!");
$chosen = $ex[$_GET['symbol']];
Here's another approach:
extract($_GET, EXTR_PREFIX_ALL, "ex");
Although it's better to use it like this just to make sure there is no security issues.
extract($_GET, EXTR_SKIP);
PHP's extract() does what exactly what you want, and you should specify "ex_" as the prefix you want.
However, there are security issues and unintended consequences to using such a function blindly, so read up on the additional paragraphs following the function parameters.
Will the below achieve what you need?
$myGetVariable = $_GET['symbol'];
$ex_{$myGetVariable} = "Something";
$_GET['symbol'] = 'APPL';
if (!empty($_GET)) {
foreach ($_GET as $k => $v) {
$var = 'ex_'.$k ;
$$var=$v;
}
}
var_dump($ex_symbol);
APPL
Let me first say I've spent a day reading three google pages of articles on this subject, as well as studied this page here.
Ok, here's my dilemma. I have two functions. Both called upon via AJAX. This first one assigns a value to the variable and the second one uses that variable. Both functions are triggered by two separate buttons and need to stay that way. The AJAX and the firing off of the functions work fine, but the variable isn't passed. Here is my code:
if( $_REQUEST["subjectLine"] ) //initiate first function
{
$CID = wpCreateChimpCampaign();
echo $CID; //this works
}
if( $_REQUEST["testEmails"] ) //initiate second function
{
echo $CID; //does not return anything but should contain "apple"
wpSendChimpTest($CID);
}
function wpCreateChimpCampaign () //first function
{
$CID = "apple";
return $CID;
}
function wpSendChimpTest ($CID) //second function
{
echo $CID; //does not return anything but should contain "apple"
}
I'm open to using a class but I haven't had much luck there either. I was hoping to solve this issue without using classes. Thanks for the help in advance!
If you are making 2 separate calls to this file, it may be helpful for you to visualise this as being 2 functions in 2 totally separate files. Although they exist in the same PHP file, because they used called in different calls, they don't retain the value of the variable $CID. Once the file has run, the variable is destroyed and when you call the file again, the value is null again.
So you need to store that variable between calls. You can either store it in a database or store it in a session variable.
So call session_start(); at the beginning of the file, then rather than use $CID, just use $_SESSION['CID'];
I'm not sure where the hold up is. The code you have will work:
$CID = wpCreateChimpCampaign(); // returns 'apple'
wpSendChimpTest($CID); // echos 'apple'
The code looks fine, but are you certain that all requirements are being met so both functions execute?
In other words are you supplying values for both $_REQUEST["subjectLine"] and $_REQUEST["testEmails"]?
Here is what I would do in JavaScript. Is there any way to do it in php?
I am working on a project that needs this functionality but cannot use JavaScript.
setInterval ( "checkHistory()", 1000 );
function checkHistory() {
if (oldHistLength != history.length) {
removegateway();
oldHistLength = history.length;
}
}
Sorry to say that it's not possible to do that using PHP. Your only option is to use JavaScript somewhere.
You can however achieve what I believe you're trying to do with another technique - PHP Sessions and Request URIs.
This involves storing the user's accessed URLs into a variable (or you could use MySQL) which can be referenced anywhere on the website within that current session.
Here's an (untested) example:
<?php
session_start();
// Retrieve/create the current list
if( isset($_SESSION['history']) ) {
$history = $_SESSION['history'];
} else {
$history = new array();
}
// Add the current URL to the history array
array_push($history, $_SERVER['REQUEST_URI']);
// Do anything else you want to here
// Store the array again
$_SESSION['history'] = $history;
?>
In your code, you can keep an array containing the values of $_SERVER['php_self'], serialize() it, and store it in a session variable. This may not be sufficient for what you are trying to do though. I'm not sure what removegateway() does, but is this code attempting to prevent the back button from being used?
If you prevent the pages from being cached, you might be able to compare the second to the last value in your array to the current page, and if they match, you detected a back button. This would only be possible if there's no way to go back to the previous page on the front end.
Preventing the back button is generally considered a Bad Thing, so it might be better to reconsider the way you are doing things and come up with a better solution.