PHP error page problem - php

I can't figure out why this script isn't working.
<?php
if (($_GET['p'] != 'index') &&
($_GET['p'] != 'reg') &&
($_GET['p'] != 'login') &&
($_GET['p'] != 'ad') &&
(!isset($_GET['p']))):
?>
<?php endif; ?>
I want to not display the error page if the $_GET is not set, which in my experience (!isset($_GET['p'])) should do.

If $_GET['p'] is not set, you can't check $_GET['p'] != 'index' and all the others. You'll have to check if it's set first:
<?php if(
! isset( $_GET['p'] ) ||
($_GET['p'] != 'index' &&
$_GET['p'] != 'reg' &&
$_GET['p'] != 'login' &&
$_GET['p'] != 'ad')
): ?>
A better solution would be to put all those values in an array, and check if $_GET['p'] is in the array:
<?php if(
! isset( $_GET['p'] ) ||
! in_array(
$_GET['p'],
array('index', 'reg', 'login', 'ad')
)
): ?>
EDIT:
Now that you provided some more info, here's what you should do:
if ( ! isset($_GET['p']) )
{
// We're at the index page, so don't display anything
}
else
{
if ( in_array( $_GET['p'], array('index', 'reg', 'login', 'ad') ) )
{
// Display your content window
}
else
{
// $_GET['p'] is not a valid value, display error
}
}

Your condition makes no sense. You're checking for 3 possible values of $_GET['p'] and then checking if $_GET['p'] is even set. Reverse your logic:
<?php
if(isset($_GET['p']))
{
// display error page
}
else
{
// do something else
}

You can check if $_GET['p'] is set by
if(isset($_GET['p']) {...}
If it's set and not empty then you can check for values that you need to check.

Try this:
<?php
$defaultvalue=0; // for example 0
$p=isset($_GET["p"]) ? $_GET["p"] : $defaultvalue;
if(($p != 'index') && ($p != 'reg') && ($p != 'login') && ($p != 'ad')):
?>

Related

How to use slug to redirect a user to a different page using wp_redirect()

I am trying to redirect users to the form page if they have not yet filled it so that if they visit the pages in my conditions, they can be redirected to back to fill the form first.
I have been able to redirect other pages properly but I'm having an issue with a page that contains a parameter.
The link https://example.com/wp-admin/admin.php?page=booknetic
I tried:
//Redirect user back to application form if they have not filled it
add_action( 'template_redirect', 'redirect_if_user_logged_in' );
function redirect_if_user_logged_in() {
if ( is_front_page() || isset($_GET['page']) || is_page('profile') || is_page('account')
&& is_user_logged_in() && $count == 0) {
wp_redirect( '/first-time-application-temp/');
exit;
}
}
AND
//Redirect user back to application form if they have not filled it
add_action( 'template_redirect', 'redirect_if_user_logged_in' );
function redirect_if_user_logged_in() {
if ( is_front_page() || $_SERVER['REQUEST_URI'] == '/wp-admin/admin.php?page=booknetic' || is_page('profile') || is_page('account')
&& is_user_logged_in() && $count == 0) {
wp_redirect( '/first-time-application-temp/');
exit;
}
}
is_front_page(), and is_page('profile') || is_page('account') && is_user_logged_in() && $count == 0)
Note* $count is a global variable that checks whether the user had an entry in the form.
How do I test whether the is_page == wp-admin/admin.php?page=booknetic ?
Could it be failing because the menu link is just a custom link that redirects to the page https://example.com/wp-admin/admin.php?page=booknetic which is an existing page created by another plugin?
add_action( 'template_redirect', 'redirect_if_user_logged_in' );
function redirect_if_user_logged_in() {
if ( (is_front_page() || isset($_GET['page']) || is_page('profile') || is_page('account') ) && is_user_logged_in() && $count == 0) {
wp_redirect( '/first-time-application-temp/');
exit;
}
}
OR
add_action( 'template_redirect', 'redirect_if_user_logged_in' );
function redirect_if_user_logged_in() {
if ( ( is_front_page() || $_SERVER['REQUEST_URI'] == '/wp-admin/admin.php?page=booknetic' || is_page('profile') || is_page('account') ) && is_user_logged_in() && $count == 0) {
wp_redirect( '/first-time-application-temp/');
exit;
}
}

Display content to diverses type of role but not all

I would like to display content to a certains kind of user (some of them custom) if they are logged-in.
Is there a way to associate :
if( is_user_logged_in() && ($user_role != "Administrator" && $user_role != "customrole") ):
// display content
else:
// redirect back to other page
wp_redirect( 'https://customurl.com' );
exit;
endif;
But when I try thsio code I have a fatal error. What do I do wrong please ?
Best regards,
Clément
if you wanna use multiple conditions you have to remove the () afther your && in the if statement and try it like this
if(is_user_logged_in() && $user_role != "Administrator" && $user_role != "customrole"){
//display content
}
else{
wp_redirect( 'https://customurl.com' );
exit;
}
Finally it was a mix, I used permission and a custom field if I want some part accessible to everyone :
$access_to_everyone = get_field('access_to_everyone');
// var_dump($access_to_everyone);
if ( $access_to_everyone == true ) {
$access = "granted";
} else {
if( is_user_logged_in() ) {
if ( !current_user_can('administrator') ) {
$access = "refused";
}
} else {
$access = "granted";
}
} else {
$access = "refused";
}
}
if ( $access == "granted" ) {
display A;
} else {
redirection";
}

Redirect to current URL, but replace string?

So I have a working redirect that currently sends customers to my homepage if they select a different city from a drop-down select. This changes some custom user meta data and uses that to determine when a redirect should occur. Simple enough.
if ( is_user_logged_in() && $metalocation_display !== 'chicago' && strpos($url,'chicago') == true) {
header('Location: example.com');
}
if ( is_user_logged_in() && $metalocation_display == 'chicago' && strpos($url,'chicago') == false) {
header('Location: example.com/chicago/');
}
What I would like to achieve is, if the customer is on the shop page, the I can redirect them to the CURRENT page they are on (not the homepage), but simply replace the city name with strpos or something similar... basically call the current URL and only replace the name of the city, regardless of where in the URL that string is.
Something like:
if ( is_user_logged_in() && $metalocation_display !== 'chicago' && strpos($url,'chicago') == true) {
header('Location: example.com');
}
if ( is_user_logged_in() && $metalocation_display == 'chicago' && strpos($url,'chicago') == false) {
header('Location: GET CURRENT URL & REPLACE CURRENT CITY (using a list of cities) OR ADD 'CHICAGO' AFTER DOMAIN IF ONE DOESN'T EXIST ');
}
Any advice?
Something like this should work (remove type declarations if using < PHP 7+):
function get_current_url( array $cities, string $target_city ): string {
// Get protocol.
$url = ( $_SERVER['HTTPS'] === 'on' ? 'https://' : 'http://' );
// Get URL parts.
$port = $_SERVER['SERVER_PORT'];
$name = $_SERVER['SERVER_NAME'];
$uri = $_SERVER['REQUEST_URI'];
// Check all cities in the array and replace if found.
foreach ( $cities as $city ) {
$uri = str_replace( $city, $target_city, $uri );
}
// Build URL with matching port.
$url .= ( $port !== '80' ? $name . ':' . $port . $uri : $name . $uri );
// Return escaped URL.
return esc_url( $url );
}
And then in your code:
if ( is_user_logged_in() && $metalocation_display !== 'chicago' && strpos($url,'chicago') == true) {
header('Location: example.com');
}
// Fill array with possible cities.
$cities = array( 'london', 'new-york', );
if ( is_user_logged_in() && $metalocation_display == 'chicago' && strpos($url,'chicago') == false) {
header('Location: ' . get_current_url( $cities, 'chicago' ) );
}
Obviously be sure to prefix the function though.

ERR TOO MANY REDIRECTS - When redirecting user based on their selected city

So I have a drop-down select that changes a custom field's value in their user profile based on their selected city. If a user chooses 'Detroit' for example, Detroit is then stored in their profile and that data is then used to redirect the customer based on their selection.
An issue I am running into seems to be if the customer selects a NEW different city on the homepage, or on a page deeper in the directory they get met with the TOO MANY REDIRECTS loop.
I understand WHY, I just can't seem to see where my issues are that are causing it.
Any help appreciated. Even if there is an easier, cleaner way to accomplish this.
add_action('wp_footer' , 'city_redirects');
function city_redirects() {
global $current_user;
global $wp;
$current_url = home_url( add_query_arg( array(), $wp->request ) );
wp_get_current_user(); // wordpress global variable to fetch logged in user info
$userID = $current_user->ID; // logged in user's ID
$metalocation_display = get_user_meta($userID, 'location_select', true); // stores the value of logged in user's meta data for 'test'.
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
//chicago REDIRECTS
if ( is_user_logged_in() && $metalocation_display == 'chicago' && strpos($url,'chicago') == false) {
header('Location: example.com/chicago/' . $_SERVER['REQUEST_URI']);
}
if ( is_user_logged_in() && $metalocation_display !== 'chicago' && strpos($url,'chicago') == true) {
header('Location: example.com/' . str_replace('chicago', '', 'example.com/chicago') . $_SERVER['REQUEST_URI']);
}
if ( is_user_logged_in() && is_front_page() && $metalocation_display !== 'chicago' && strpos($url,'chicago') == true) {
header('Location: example.com/');
}
//detroit REDIRECTS
if ( is_user_logged_in() && $metalocation_display == 'detroit' && strpos($url,'detroit') == false) {
header('Location: example.com/detroit/' . $_SERVER['REQUEST_URI']);
}
if ( is_user_logged_in() && $metalocation_display !== 'detroit' && strpos($url,'detroit') == true) {
header('Location: example.com/' . str_replace('detroit', '', 'example.com/detroit') . $_SERVER['REQUEST_URI']);
}
if ( is_user_logged_in() && is_front_page() && $metalocation_display !== 'detroit' && strpos($url,'detroit') == true) {
header('Location: example.com/');
}
//philadelphia REDIRECTS
if ( is_user_logged_in() && $metalocation_display == 'philadelphia' && strpos($url,'philadelphia') == false) {
header('Location: example.com/philadelphia/' . $_SERVER['REQUEST_URI']);
}
if ( is_user_logged_in() && $metalocation_display !== 'philadelphia' && strpos($url,'philadelphia') == true) {
header('Location: example.com/' . str_replace('philadelphia', '', 'example.com/philadelphia') . $_SERVER['REQUEST_URI']);
}
if ( is_user_logged_in() && is_front_page() && $metalocation_display !== 'philadelphia' && strpos($url,'philadelphia') == true) {
header('Location: example.com/');
}
}

PHP Multiple Drop Down Variables Empty

I am performing some validation on three different drop down menus. If all three variables are NULL, I am wanting to direct the user to the specific error. I am asking the visitor to choose one of the dropdowns, not all three. Here is my line of code that I cant see to get working:
if ( $dropdown1 == 'NULL', $dropdown2 == 'NULL', $dropdown3 == 'NULL' ) {
echo '<h1>Error Edited For the Sake of Brevity</h1>';
}
else
Apreciate any thoughts anyone could share with me.
Thanks Again,
--Matt
Well, you're probably getting all of the values from $_REQUEST/$_GET/$_POST (I'm using $_REQUEST below because it works for all), for that you'd use isset.
if( !isset( $_REQUEST[ 'dropdown1' ] ) &&
!isset( $_REQUEST[ 'dropdown2' ] ) &&
!isset( $_REQUEST[ 'dropdown3' ] ) )
{
// none of them are set.
}
If you're actually looking to see if the variables themselves are set to null, then you should use is_null
if( is_null( $dropdown1 ) &&
is_null( $dropdown2 ) &&
is_null( $dropdown3 ) )
{
// none of them are set.
}
Finally, if you have reason to believe that they will be the STRING value 'NULL' then you can use == (this is a comparably rare circumstance):
if( 'NULL' == $dropdown1 &&
'NULL' == $dropdown2 &&
'NULL' == $dropdown3 )
{
// none of them are set.
}
You can change any of the above to test to see if any of them are not set by using || (or) instead of && (and):
if( 'NULL' == $dropdown1 ||
'NULL' == $dropdown2 ||
'NULL' == $dropdown3 )
{
// one of them is not set.
}
I assume you're trying to do a boolean AND comparison, which in PHP is:
if ( $dropdown1 == 'NULL' && $dropdown2 == 'NULL' && $dropdown3 == 'NULL' )...
if ( $dropdown1 == 'NULL', $dropdown2 == 'NULL', $dropdown3 == 'NULL' ) {
should be
if ( $dropdown1 == 'NULL' || $dropdown2 == 'NULL' || $dropdown3 == 'NULL' ) {
In this case if any of the variables is null, the it will display your error message
You shouldn't be using commas to separate the conditions
if ( $dropdown1 == 'NULL' || $dropdown2 == 'NULL' || $dropdown3 == 'NULL' )
if you want to give error message when all of the dropdowns are null then you should use this condition
if($dropdown1 == 'NULL' && $dropdown2 == 'NULL' && $dropdown3 == 'NULL' ){
echo $error_msg;
}

Categories