$GLOBALS between pages PHP 5.4 index error - superglobals

It was my understanding that $GLOBALS was a super global and enabled a scope to extend across all pages. I'm a noob to PHP and I have been trying to pass a value from one page to another like so:
A.php - reads
$GLOBALS['A'] = 'Passed A';
echo 'Main says:'.$GLOBALS['A'].'<br />';
disp();
function disp(){
echo 'Function says:'.$GLOBALS['A'].'<br />';
}
b.php reads
<?php
echo 'B says:'.$GLOBALS['A'].'<br />';
$code = disp();
function disp(){
echo 'Function in B says:'.$GLOBALS['A'].'<br />';
}
?>
When a.php is called I get
Main says:Passed A
Function says:Passed A
when it chains to b.php I get
Notice: Undefined index: A in (omitted path) \b.php on line 3
B says:
Notice: Undefined index: A in (omitted path) \b.php on line 8
Function in B says
Am I correct in saying this should work? If not is there a way to send a variable from one page to the next without including them in the same file? Any help greatly appreciated.

It is true That $GLOBALS can hold the value and it is available in all scopes throughout a script. Thats why b.php become malfunction.Because $GOLBALS must be pass to b.php in order to get the value from $GLOBAL and you have to catch this with variable.
To pass the value From a page to page you can use the followings
Session (for server-side )
$_POST
cookies(for client-side)

Related

How can I add server field to post on data in PHP?

Welcome to use the WordPress template developer allows the addition of servers to watch movies I have some problems are when you add a server and update the article is updated but returns the right empty or not added and then when you delete the server is not deleted I checked the error record and this is the result
[21-Sep-2018 10:42:50 UTC] PHP Warning: array_combine() expects parameter 2 to be array, boolean given in /home3/mysite/public_html/wp-content/themes/movie/functions.php on line 154
File functions.php
https://3bdo.info/functions.zip
The linked zip file seams to be broken so I cannot extract the file.
But in general the error message says, that you have to use two arrays to call array_combine. You seam to try to combine an array (which is like a list) and a boolean (which is like yes/no). And that is not possible.
$android_servers_title2 = servers_get_meta( 'android_download_server_title' );
$android_servers_code2 = servers_get_meta( 'android_download_server_link' );
/* foreach($servers_title as $server_title)
{
echo "<label for='servers_server_title'>Server Title</label>";
echo " <br><input type='text' name='servers_server_title[]' id='servers_server_title' value='" . $server_title . "'><br>";
} */
if($android_servers_title2){
$android_arraye2 = array_combine($android_servers_title2, $android_servers_code2);
}else {
$android_arraye2 = false;
}
This is basically your problem. servers_get_meta is returning 'false' I would imagine. Check that 'android_download_server_link' is set. you may wish to change the if statement code to detect a false
if($android_servers_title2 && $android_servers_code2){

fatal error when calling wp_create_nonce in options-init.php

I am developing a wordpress plugin using the wordpress boilerplate template. Additionally, I am using Redux Framework as an options framework. I load the options-init.php in the load_dependencies() function of the main include file.
Everything works fine. However, when calling wp_create_nonce in options-init.php I get a Fatal error: Uncaught Error: Call to undefined function wp_create_nonce()
Why is it and how to solve it?
wp_create_nonce() is a pluggable function loaded after plugins are loaded.
BEFORE call pluggable.php file:
require_once( ABSPATH . 'wp-includes/pluggable.php' );
Thanks.
I found the solution myself, although not sure if it is correct/best practice.
I moved the include of the redux framework into a seperate class which I call via action hook on ''plugins_loaded'
//Initiate Redux Framework
$this->loader->add_action( 'plugins_loaded', $plugin_admin_redux, 'init_redux_framework' );
Inside the method init_redux_framework() I just put the require once to the admin-init.php
Interestingly though that adding it on 'init' instead of 'plugins_loaded' was too late for Redux to be initialized.
Why do you need wp_create_nonce() inside plugin?
Nonce is usually included in a hidden HTML form field or as a part of an URL and therefore sent with a request by submitting a form field or visiting a link:
<?php
// Create an nonce for a link.
// We pass it as a GET parameter.
// The target page will perform some action based on the 'do_something' parameter.
$nonce = wp_create_nonce( 'my-nonce' );
?>
<a href='myplugin.php?do_something=some_action&_wpnonce=<?php echo $nonce; ?>'>Do some action</a>
And inside plugin files you can verify nounce:
wp_verify_nonce( $nonce, 'my-nonce' );
wp_create_nonce() is a pluggable function loaded after plugins are loaded.
Be sure to call your class method on proper hook, 'init' (or later) is a good place: once your function output something (a form) there is no reason to run earlier than that.
wp_verify_nonce() receives 2 arguments:
$nonce
$action
1st argument is the nonce, and comes from the request
In fact, wp_verify_nonce() have to be used like so:
// here I assume that the form is submitted using 'post' as method
$verify = wp_verify_nonce($_POST['message-send']);
So the first argument passed to wp_verify_nonce() is exactly the value that is present in the hidden field
2nd argument: the wp_create_nonce() method
Regarding the second argument, it depends on how you build the nonce value.
E.g. if you did:
<?php $nonce = wp_create_nonce( 'custom-action' ); ?>
<input type="hidden" name="message-send" value="<?php echo $nonce ?>" />
Then you need to do:
$verify = wp_verify_nonce( $_POST['message-send'], 'custom-action' );
So, the second argument is what was used as argument to wp_create_nonce().
2nd argument: the wp_nonce_field() method
If you created the nonce using wp_nonce_field() like:
wp_nonce_field( 'another_action', 'message-send' );
Then you need to verify the nonce like so:
$verify = wp_verify_nonce( $_POST['message-send'], 'another_action' );
So, this time, the action is whatever passed as first argument to wp_nonce_field().
Recap:
To pass wp_verify_nonce() validation you need to pass 2 arguments to the function, one is the value in the nonce hidden field, the other is the action, and depends on how the nonce value was built.

Storing single_tag_title in variable

In Wordpress, the following will echo the currently chosen tag in an archive listing:
single_tag_title();
Does anybody know how to store it in a variable, so that i can echo out the same string using an ordinary php variable like this:
echo $tagSlug; //This shows the same string as the single_tag_title() function
I have tried this:
$tagSlug = single_tag_title();
echo $tagSlug;
But it doesn't work.
single_tag_title accepts two arguments. $prefix and $display. $prefix sets the string which should be echoed/returned before the tag, and $display determines whether the tag is echoed or returned as a variable. See the documentation here.
$tagSlug = single_tag_title("", false);
echo $tagSlug;

simple_html_dom: Call to a member function find() on a non-object in

This code has been working good for months. The output consisted of a few lines containing statistics for a given username. The website from which the html page is taken is not down, and the content of the page in file_get_html hasn't changed.
All of a sudden (I checked and nobody modified it) it stopped working. Here's the relevant part:
[...]if ($FileAge > ($expiretime * 60) || 0 == filesize($cachename))
{
include_once('simple_html_dom.php');
$html = file_get_html('http://www.foo.com/search?what=user%3A'.YOUR_USER.'&search=Search');
var_dump($html); //TEST
$link = $html->find('.likeh4 lightGrey.', 0)->find('a', 0)->href; // Get the last activity link
[...]
The error log says:
[02-Feb-2013 17:02:19 Europe/Berlin] PHP Fatal error: Call to a member function find() on a non-object in /foo.php on line 22 (the line with $link).
var_dump($html) gives bool(false)
I have a similar script which parses an html page from another website. It stopped working as well.
[...]include_once('simple_html_dom.php');
$html = file_get_html('http://my.flightmemory.com/'.FLIGHTMEMORY_USER);
$chilometri_table = $html->find('table', 2); [...]
I tried to save on my webserver one of those html pages and I don't get such error.
Did my host disable some php function for security reasons? (actually, file_get_html comes from simple_html_dom and not from php native functions)
Any hints?
Thanks
It is probably way too late but:
Simple_html_dom has a constant to check given html size - MAX_FILE_SIZE. by default it's 600KB. It's enough for most cases, but if your given html is bigger than that, it will fail and returns false and causes that fatal error.
If you try to get [href]
I had same problem and fixed it.
need valid it's a simple_html_dom_node
if(is_a($html->find('.likeh4 lightGrey. a', 0),'simple_html_dom_node' )
$link = $html->find('.likeh4 lightGrey. a', 0)->href;
OR
foreach ( $$html->find('.likeh4 lightGrey. a') as $links ) {
$link =$links->href;
}

how to retrieve custom field at root directory for wordpress?

i have create a redir.php and placed in the home directory of wordpress to do the redirect. I want to pass in the post id and then retrieve a custom field value of the post and feed into header('location:'.$url);
www.mysite.com/redir.php?id=30
in redir.php will retrieve post id=30 custom field value and pass it into $url.
this is what I have, but it's not working. It gives me "Parse error: parse error in \redir.php on line 5".
it looks like wordpress environment is not being loaded.
<?php
require('./wp-blog-header.php');
$id = $_GET['id'];
$url= get_field('deal_http_link',post->$id);
header('Location:'.$url);
?>
thanks
Your script has multiple issues:
There is whitespace before the opening <?php tag, so the redirect wouldn't work because the headers will have already been sent. Instead, <?php should be the very first thing in the file.
post->$id is invalid syntax. You probably meant the $id variable which you defined in the preceding line.
To retrieve the value of a custom field, use get_post_meta(), not get_field().
Try something like this instead:
<?php
require('./wp-blog-header.php');
$id = $_GET['id'];
$url = get_post_meta($id, 'deal_http_link', true);
header('Location: ' . $url);
exit();

Resources