$subjectDirectory = '../blogtext/';
$subjectHandle = opendir( $subjectDirectory );
$fileName = $_SERVER['PHP_SELF'];
$tempArray = explode( '-', $fileName );
$finalNum = '-'.$tempArray[1].'-';
$subjectFile;
if( $subjectHandle = opendir( '../blogtext/' ) )
{
/* If you echo $finalNum here, you get '-0-' on the page. */
while( false !== ( $subjectFile = readdir( $subjectHandle ) ) )
{
/* If you echo $finalNum here, you get '-0--0--0--0--0-' on the page. */
if( $subjectFile != '.' && $subjectFile != '..' && !is_dir( $subjectFile ) && strpos( $subjectFile, $finalNum ) )
{
include( $subjectFile );
}
}
closedir( $subjectHandle );
}
Basically, what I'm trying to do is;
get -NUMBER- code from the current file name ( -0-example.php ), and then scan through the directory ( $subjectDirectory ) for the file name that begins with the same code. Then include the file.
I'm unable to do so because the $finalNum changes the code to "code 5 times in a row", so I can't find the right file to include.
The reason it's not working is because readdir() returns the file name only, not the full path.
Try this:
include( $subjectDirectory . $subjectFile );
Re: /* If you echo $finalNum here, you get '-0--0--0--0--0-' on the page. */
The reason you get this output is because you are echoing $finalNum 5 times. The value of $finalNum is not changing.
EDIT:
I found one more issue in your if statement. strpos( $subjectFile, $finalNum ) will return 0 if the $subjectFile starts with $finalNum.
Use this instead.
if( $subjectFile != '.' && $subjectFile != '..' && !is_dir( $subjectFile ) && strpos( $subjectFile, $finalNum ) !== false )
Related
Good day,
I would like to know if there is any function for making the difference between this:
?param=
and this:
?param
Because I would like my script to detect if value is empty (first example), or if it's not given (second example).
I made a test with the following functions but I could not find what I want:
if( isset( $_GET['param'] ) ) {
echo '<div>isset</div>';
if( empty( $_GET['param'] ) ) {
echo '<div>empty</div>';
} else {
echo '<div>not empty</div>';
}
if( is_null( $_GET['param'] ) ) {
echo '<div>null</div>';
} else {
echo '<div>not null</div>';
}
if( defined( $_GET['param'] ) ) {
echo '<div>defined</div>';
} else {
echo '<div>undefined</div>';
}
} else {
echo '<div>not set</div>';
}
Any idea? Thank you in advance.
EDIT
Solution:
( strpos( '?'.$_SERVER['QUERY_STRING'], '?get=' ) !== false ) || ( strpos( '?'.$_SERVER['QUERY_STRING'], '&get=' ) !== false )
You can test with something like if( $_GET['param'] ) which will give true if Param is declared (even if it is an empty string) and false if not declared.
I am using the following code to display widgets by permalinks...
<?php
global $post;
$permalink = get_permalink( $post->ID );
if ( strpos( $permalink, '/one/' ) !== false || strpos( $permalink, '/one-two/' ) !== false ) {
echo dynamic_sidebar( 'disclaimer_cop' );
} elseif ( strpos( $permalink, '/two/' ) !== false || strpos( $permalink, '/two-one/' ) !== false ) {
echo dynamic_sidebar( 'disclaimer' );
} ?>
This code is dynamically displaying the widgets on the correct pages but when viewing the page the widget position is being followed by a random "1" in the HTML, like this.
<div class="widget_text disclaimer"> <div class="textwidget custom-html-widget">*Disclaimer text.</div>
1
Any ideas as to what is wrong?
Remove the echo. Looks like you're outputting the result as well as the content.
<?php
global $post;
$permalink = get_permalink( $post->ID );
if ( strpos( $permalink, '/one/' ) !== false || strpos( $permalink, '/one-two/' ) !== false ) {
dynamic_sidebar( 'disclaimer_cop' );
} elseif ( strpos( $permalink, '/two/' ) !== false || strpos( $permalink, '/two-one/' ) !== false ) {
dynamic_sidebar( 'disclaimer' );
}
?>
I am not sure how you are implentening on your case the code you specified. If you do it via add_shortcode() and then you call your shortcode on a widget you will just need to change the echo with a return.
Hope that helps.
Cheers!
When I installed WooCommerce on a WordPress page I got the chance to manage a little while ago, I started getting these errors whenever I go to a subpage:
Warning: preg_match() [function.preg-match]: Compilation failed: nothing to ?repeat at offset 1 in /var/www/watertours.dk/public_html/wp-includes/class-wp.php on line 222
Warning: preg_match() [function.preg-match]: Compilation failed: nothing to repeat at offset 1 in /var/www/watertours.dk/public_html/wp-includes/class-wp.php on line 223"
It even shows up in the dashboard occasionally.
I have found this guide which I have already tried several times:
step 0: if possible, backup your WP installation folder.
step 1: temporary disable all the plugins (important step)
step 2: in WordPress admin dashboard, go to Settings -> Permalinks
step 3: remember or note down somewhere what you have in the custom permalinks field: http://awesomescreenshot.com/0534epzk0c 96
step 4: temporary enable (switch to) the default permalink: http://awesomescreenshot.com/0f74epyi15 79 Click Save Changes button.
step 5: verify the website is working now (not everything, because the plugins are disabled, but the preg_match error should be gone)
step 6: switch back to the custom permalinks setting you had at step 3
step 7: enable back all the plugins
The error should be gone."
It works for a little while (two minutes or so) and then those two errors start popping up again.
I am thinking of just remaking the WordPress site from the ground up since it is quite a mess anyway. But if anyone has a solution, I would be more than grateful. :)
EDIT:
* Parse request to find correct WordPress query.
*
* Sets up the query variables based on the request. There are also many
* filters and actions that can be used to further manipulate the result.
*
* #since 2.0.0
*
* #global WP_Rewrite $wp_rewrite
*
* #param array|string $extra_query_vars Set the extra query variables.
*/
public function parse_request( $extra_query_vars = '' ) {
global $wp_rewrite;
/**
* Filters whether to parse the request.
*
* #since 3.5.0
*
* #param bool $bool Whether or not to parse the request. Default true.
* #param WP $this Current WordPress environment instance.
* #param array|string $extra_query_vars Extra passed query variables.
*/
if ( ! apply_filters( 'do_parse_request', true, $this, $extra_query_vars ) ) {
return;
}
$this->query_vars = array();
$post_type_query_vars = array();
if ( is_array( $extra_query_vars ) ) {
$this->extra_query_vars = & $extra_query_vars;
} elseif ( ! empty( $extra_query_vars ) ) {
parse_str( $extra_query_vars, $this->extra_query_vars );
}
// Process PATH_INFO, REQUEST_URI, and 404 for permalinks.
// Fetch the rewrite rules.
$rewrite = $wp_rewrite->wp_rewrite_rules();
if ( ! empty( $rewrite ) ) {
// If we match a rewrite rule, this will be cleared.
$error = '404';
$this->did_permalink = true;
$pathinfo = isset( $_SERVER['PATH_INFO'] ) ? $_SERVER['PATH_INFO'] : '';
list( $pathinfo ) = explode( '?', $pathinfo );
$pathinfo = str_replace( '%', '%25', $pathinfo );
list( $req_uri ) = explode( '?', $_SERVER['REQUEST_URI'] );
$self = $_SERVER['PHP_SELF'];
$home_path = trim( parse_url( home_url(), PHP_URL_PATH ), '/' );
$home_path_regex = sprintf( '|^%s|i', preg_quote( $home_path, '|' ) );
// Trim path info from the end and the leading home path from the
// front. For path info requests, this leaves us with the requesting
// filename, if any. For 404 requests, this leaves us with the
// requested permalink.
$req_uri = str_replace( $pathinfo, '', $req_uri );
$req_uri = trim( $req_uri, '/' );
$req_uri = preg_replace( $home_path_regex, '', $req_uri );
$req_uri = trim( $req_uri, '/' );
$pathinfo = trim( $pathinfo, '/' );
$pathinfo = preg_replace( $home_path_regex, '', $pathinfo );
$pathinfo = trim( $pathinfo, '/' );
$self = trim( $self, '/' );
$self = preg_replace( $home_path_regex, '', $self );
$self = trim( $self, '/' );
// The requested permalink is in $pathinfo for path info requests and
// $req_uri for other requests.
if ( ! empty( $pathinfo ) && ! preg_match( '|^.*' . $wp_rewrite->index . '$|', $pathinfo ) ) {
$requested_path = $pathinfo;
} else {
// If the request uri is the index, blank it out so that we don't try to match it against a rule.
if ( $req_uri == $wp_rewrite->index ) {
$req_uri = '';
}
$requested_path = $req_uri;
}
$requested_file = $req_uri;
$this->request = $requested_path;
// Look for matches.
$request_match = $requested_path;
if ( empty( $request_match ) ) {
// An empty request could only match against ^$ regex
if ( isset( $rewrite['$'] ) ) {
$this->matched_rule = '$';
$query = $rewrite['$'];
$matches = array( '' );
}
} else {
foreach ( (array) $rewrite as $match => $query ) {
// If the requested file is the anchor of the match, prepend it to the path info.
if ( ! empty( $requested_file ) && strpos( $match, $requested_file ) === 0 && $requested_file != $requested_path ) {
$request_match = $requested_file . '/' . $requested_path;
}
if ( preg_match( "#^$match#", $request_match, $matches ) || // Line 222
preg_match( "#^$match#", urldecode( $request_match ), $matches ) ) { // Line 223
if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) ) {
// This is a verbose page match, let's check to be sure about it.
$page = get_page_by_path( $matches[ $varmatch[1] ] );
if ( ! $page ) {
continue;
}
$post_status_obj = get_post_status_object( $page->post_status );
if ( ! $post_status_obj->public && ! $post_status_obj->protected
&& ! $post_status_obj->private && $post_status_obj->exclude_from_search ) {
continue;
}
}
// Got a match.
$this->matched_rule = $match;
break;
}
}
}
if ( isset( $this->matched_rule ) ) {
// Trim the query of everything up to the '?'.
$query = preg_replace( '!^.+\?!', '', $query );
// Substitute the substring matches into the query.
$query = addslashes( WP_MatchesMapRegex::apply( $query, $matches ) );
$this->matched_query = $query;
// Parse the query.
parse_str( $query, $perma_query_vars );
// If we're processing a 404 request, clear the error var since we found something.
if ( '404' == $error ) {
unset( $error, $_GET['error'] );
}
}
// If req_uri is empty or if it is a request for ourself, unset error.
if ( empty( $requested_path ) || $requested_file == $self || strpos( $_SERVER['PHP_SELF'], 'wp-admin/' ) !== false ) {
unset( $error, $_GET['error'] );
if ( isset( $perma_query_vars ) && strpos( $_SERVER['PHP_SELF'], 'wp-admin/' ) !== false ) {
unset( $perma_query_vars );
}
$this->did_permalink = false;
}
}```
I have this code that is ment to stop people entering dots and slashes into query strings.
The string is defined to a variable, then if the variable containes a '.' or a '/' i would like to replace them with forbidden and main.
<?php
if( strpos( $page, '/' ) === true ) { $page = 'forbidden'; $inner_page = 'main'; }
elseif( strpos( $page, '.' ) === true ) { $page = 'forbidden'; $inner_page = 'main'; }
elseif( strpos( $inner_page, '/' ) === true ) { $page = 'forbidden'; $inner_page = 'main'; }
elseif( strpos( $inner_page, '.' ) === true ) { $page = 'forbidden'; $inner_page = 'main'; }
?>
This code (isAllowedURL.php) is being loaded into this code:
<?php
$page = 'home';
$inner_page = 'main';
if( isset( $_GET['page'] ) )
{
$page = htmlspecialchars( $_GET['page'] );
}
if( isset( $_GET['subpage'] ) )
{
$inner_page = htmlspecialchars( $_GET['subpage'] );
}
include ('/indexBuilder/linkSecurity/isAllowedURL.php');
?>
As you can see, it changes the variables to the query if it exists and i would like to change it back if the variable containes the banned characters.
Why does this code not function as wanted?
-I changed the true to false and every page was made forbidden, changed it back and none where.
SOLVED:
You need to use !== to see if they are present. Also you cannot use true with strpos.
if( strpos( $page, '/' ) !== false ) { $page = 'forbidden'; $inner_page = 'main'; }
I have a question about php include. Here goes the existing code that i have.
<?php
srand();
$files = array("folder/content1.php", "folder/content2.php", "folder/content3.php", "folder/content4.php");
$rand = array_rand($files);
include ($files[$rand]);
?>
This code actually works very well for me now as the content displaying randomly. But, i would like to get something better. Because, by using this code, everytime i have added a new content like content5.php, content6.php and so on, i have to bother to update the code above to make new content.php appear.
Is there any solution i can have, to not to bother the code above everytime i add a new content.php and the new content.php appears automatically when added? Is it possible?
Updated: New testing code(tested failed again with part of my page mising)
<?php
$sDirectory = 'myfolder';
if( is_dir( $sDirectory ) )
{
$rDir = opendir( $sDirectory );
while( ( $sFile = readdir( $rDir ) ) !== FALSE )
{
if( ( $sFile == '.' ) || ( $sFile === '..' ) )
{
continue;
}
$aFiles[] = $sDirectory . '/' . $sFile;
}
}
$sRandom = array_rand( $aFiles );
require_once( $aFiles[ $sRandom ] );
?>
$sDirectory = 'includes';
if( is_dir( $sDirectory ) )
{
$rDir = opendir( $sDirectory );
while( ( $sFile = readdir( $rDir ) ) !== FALSE )
{
if( ( $sFile == '.' ) || ( $sFile === '..' ) )
{
continue;
}
$aFiles[] = $sDirectory . '/' . $sFile;
}
}
$sRandom = array_rand( $aFiles );
require_once( $aFiles[ $sRandom ] );