I'm learning and I need my index to run my pages instead of the links using php if statements and I'm missing something in my code. It just runs my header over and over again.
my if statements:
<?php
if (!isset($_GET['page']))
{
require('index.php');
}
else if ($_GET['page'] == 'aboutus') {
if (!isset($_GET['action'])) {
require('aboutus.php');
} else if ($_GET['action'] == 'store') {
require('store.php');
} else if ($_GET['action'] == 'contactus') {
require('contactus.php');
}
else
{
require('index.php');
}
}
else
{
require('index_.php');
}
?>
My nav bar:
<ul class="nav">
<li>Home</li>
<li>About</li>
<li>Current Items</li>
<li>Contact Us</li>
</ul>
I know I'm missing something, I'm just not sure what.
If I understood correctly that you wish to load a new file based upon the querystring parameter page ( it is unclear where the querystring parameter action comes from or how it is used ) then perhaps this might help.
<?php
/* set a variable for the querystring parameter "page" */
$page=isset( $_GET['page'] ) ? filter_input( INPUT_GET, 'page', FILTER_SANITIZE_STRING ) : false;
if( !$page ) $file='index';
else {
$pages=array(/* possible files to include */
'aboutus','store','contactus'
);
/* ensure user supplied GET variable is in desired format */
$page = strtolower( trim( pathinfo( $page, PATHINFO_FILENAME ) ) );
/* Find page in array of pages - or use default "index" */
if( in_array( $page, $pages ) )$file = $pages[ array_search( $page, $pages ) ];
else $file = 'index';
}
require sprintf( '%s.php', $file );
?>
The above could be simplified slightly like so:
<?php
/* set a variable for the querystring parameter "page" */
$page=isset( $_GET['page'] ) ? filter_input( INPUT_GET, 'page', FILTER_SANITIZE_STRING ) : false;
$pages=array(/* possible files to include */
'aboutus','store','contactus'
);
/* ensure user supplied GET variable is in desired format */
$page = strtolower( trim( pathinfo( $page, PATHINFO_FILENAME ) ) );
/* Find page in array of pages - or use default "index" */
if( in_array( $page, $pages ) )$file = $pages[ array_search( $page, $pages ) ];
else $file = 'index';
require sprintf( '%s.php', $file );
?>
why is that??? you can implement Single Page Application that url only change ater hashTag like :
link.url/index#about-us
link.url/index#store
and ...
about your problem , after require your code continueus , you must die; after each require to finish loop. just made it a little prettier.
<?php
if (!isset($_GET['page'])) {
require('index.php');die;
}
if ($_GET['page'] != 'aboutus') {
require('index_.php');die;
}
// where is your action variable in your code?????????????
// it's about us
if (!isset($_GET['action'])) {
require('aboutus.php');die;
}
switch ($_GET['action']) {
case 'store':
require('store.php');die;
break;
case 'contactus':
require('contactus.php');die;
break;
default:
die("route is not defined");
break;
}
?>
Related
I have set a cookie in my functions.php as follows:
add_action('send_headers', 'sfdc_cookie');
function sfdc_cookie() {
if( isset($_GET['sfdc']) && '' != $_GET['sfdc'] ) {
if( empty($_COOKIE['sfdc']) ){
if ($_GET['sfdc'] == 'mim'):
setcookie('sfdc', 'cookievalue', time()+3156000, '/', 'www.mydomain.com' );
//$_COOKIE['sfdc'] = 'cookie';
endif;
}
}
echo $_COOKIE['sfdc'];
}
if I access any page on the site with ?sfdc=mim, the value is shown. I assumed the cookie is set at that point, and when I check the stored cookies from the Chrome browser, it appears to be set. However, if the querystring is absent, it is as if the cookie does not exist. I cannot even echo the cookie onto the page.. Why can't I echo the value when the querystring is not present in the URL?
I have changed my add_action from send_headers to init with the same result.
You should fire up this function like this:
add_action( 'wp_enqueue_scripts', 'sfdc_cookie' ); // <-- this starts the function with the same name in the location named 'wp_enqueue_scripts'
function sfdc_cookie() {
if( isset($_GET['sfdc']) && '' != $_GET['sfdc'] ) {
if( empty($_COOKIE['sfdc']) ){
if ($_GET['sfdc'] == 'mim')
{
setcookie('sfdc', 'cookievalue', time()+3156000, '/', 'www.mydomain.com' );
//$_COOKIE['sfdc'] = 'cookie';
}
}
}
if you want start cookie on page and make it available in others, you should do it like this:
add_action( 'init', 'sfdc_cookie' );
function sfdc_cookie() {
if( !isset($_COOKIE['sfdc']) ) {
setcookie('sfdc', 'cookievalue', time()+3156000, '/', 'www.mydomain.com' );
}
}
I have a WordPress Child Theme and I use a php file as the template for a particular page.
I am trying to implement an API for a plugin called GeoIP Detection. Please see the PHP file I am using on my site below. The API I am trying to apply is "Redirect depending on country" located here
When I load the script, I am supposed to be redirected to https://www.google.com.sg However, it does not do so.
Thank you.
My PHP
<?php /* Template Name: GeoIPDetectionv3 */
add_action('template_redirect', 'geoip_redirect', 5);
function geoip_redirect(){
if (is_admin())
return;
// This condition prevents a redirect loop:
// Redirect only if the home page is called. Change this condition to the specific page or URL you need.
if (!is_page(90))
return;
if (!function_exists('geoip_detect2_get_info_from_current_ip'))
return;
$userInfo = geoip_detect2_get_info_from_current_ip();
$countryCode = $userInfo->country->isoCode;
switch ($countryCode) {
case 'DE':
$url = '/germany';
break;
case 'US':
$url = '/usa';
break;
case 'SG':
$url = 'https://www.google.com.sg';
break;
default:
$url = 'https://www.google.com.sg';
}
if ($url) {
wp_redirect(get_current_blog_id(null, $url));
exit;
}
}
Use a single PHP tag, and make sure the last portion of your code is in fact inside a PHP tag. Currently it is not, so it's parsed as plain text.
UPDATE: I've cleaned this up a bit for you and updated the code to reflect your revised question; i.e., following our comments below.
<?php /* Template Name: GeoIPDetectionv3 */
add_action('template_redirect', 'geoip_redirect', 5);
function geoip_redirect(){
if ( is_admin() ) {
return; // Not applicable.
}
if ( 123 !== get_current_blog_id() ) {
return; // Not on blog ID 123.
}
if ( ! is_page( 90 ) ) {
return; // Not a specific page ID on this blog.
}
if ( ! function_exists( 'geoip_detect2_get_info_from_current_ip' ) ) {
return;
}
$userInfo = geoip_detect2_get_info_from_current_ip();
$countryCode = $userInfo->country->isoCode;
switch ($countryCode) {
case 'DE':
$redirect_to = '/germany';
break;
case 'US':
$redirect_to = '/usa';
break;
case 'SG':
$redirect_to = 'https://www.google.com.sg';
break;
default:
$redirect_to = 'https://www.google.com.sg';
}
if ( ! empty( $redirect_to ) ) {
if ( stripos( $redirect_to, 'http' ) === 0 ) {
wp_redirect( $redirect_to ); // Full URL.
} else {
wp_redirect( home_url( $redirect_to ) ); // Local /path.
}
exit;
}
}
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 am using Instant-QA theme, need to update the recent Post widget with image of the user who has asked the questions. I am using the below code in functions.php of subdomain of wordpress website. Currently its showing the simple post--- Any guess in below code-- ?
function print_requested_template_part() {
// Respond only to requests from the same address...
if ( $_SERVER['REQUEST_METHOD'] == 'GET' && isset($_GET['including_template_part']) && isset($_GET['load_part']) && $_GET['load_part'] != '' ) {
$part = $_GET['load_part'];
$func = 'render_' . str_replace('-', '_', $part); // if you have declared a function called "render_footer_include", then "?load_part=footer_include"
if ( function_exists($func) ) {
// Allow for passing parameters to the function
if ( isset($_GET['params']) ) {
$params = $_GET['params'];
$params = ( strpos($params, ',') !== false )? explode(',', $params) : array($params);
call_user_func_array($func, $params);
} else {
call_user_func($func);
}
}
exit; // if we don't exit here, a whole page will be printed => bad! it's better to have empty footer than a footer with the whole main site...
}
}
add_action('init', 'print_requested_template_part', 1);
function render_my_recent_posts( $numberposts = 5 ) { ?>
<ul>
<?php
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ) {
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> </li> ';
}
?>
</ul><?php
}
DO not waste you time to indulge urself in the above code. Usel below plugin and gets the immediate output.
http://wordpress.org/extend/plugins/recent-comments-with-avatars/ Use this plugin
Thankyou
I have been using the following script to create sitemaps for my clients websites. The issue is it does not work for every site. I have found that many if not all the sites hosted on godaddy do not spider. If anyone can see an error in my script or know what is causing the fault I would greatly appreciate the help.
Thanks in advance
set_time_limit(0);
class spider_man
{
var $url;
var $limit;
var $cache;
var $crawled;
var $banned_ext;
var $domain;
function spider_man( $url, $banned_ext, $limit ){
$this->domain = $url;
$this->url = 'http://'.$url ;
$this->banned_ext = $banned_ext ;
$this->limit = $limit ;
if( !fopen( $this->url, "r") ) return false;
else $this->_spider($this->url);
}
function _spider( $url ){
$this->cache = #file_get_contents( urldecode( $url ) );
if( !$this->cache ) return false;
$this->crawled[] = urldecode( $url ) ;
preg_match_all( "#href=\"(https?://[&=a-zA-Z0-9-_./]+)\"#si", $this->cache, $links );
if ( $links ) :
foreach ( $links[1] as $hyperlink ){
if(strpos($hyperlink,$this->domain)===false){ break; }
else{
$this->limit--;
if( ! $this->limit ) return;
if( $this->is_valid_ext( trim( $hyperlink ) ) and !$this->is_crawled( $hyperlink ) ) :
$this->crawled[] = $hyperlink;
echo "Crawling $hyperlink<br />\n";
unset( $this->cache );
$this->_spider( $hyperlink );
endif;
}
}
endif;
}
function is_valid_ext( $url ){
foreach( $this->banned_ext as $ext ){
if( $ext == substr( $url, strlen($url) - strlen( $ext ) ) ) return false;
}
return true;
}
function is_crawled( $url ){
return in_array( $url, $this->crawled );
}
}
$banned_ext = array(".dtd",".css",".xml",".js",".gif",".jpg",".jpeg",".bmp",".ico",".rss",".pdf",".png",".psd",".aspx",".jsp",".srf",".cgi",".exe",".cfm");
$spider = new spider_man( 'domain.com', $banned_ext, 100 );
print_r( $spider->crawled );
When you access a site using fopen() of file_get_contents() you don't send AGENT or REFERRER or other header information. It's blatently obvious that this is an automated script.
You need to look at sending context with your fopen (check the docs and read the context section) or, better still, using CURL. This allows you to set the agent and referrer headers to simulate a browser.