I need help here.
I have taken 1-2 days to figure out how to redirect a user from one page to another page AFTER a few or 10 secs
I know there is sleep() function I need to be fixed but where, I am not sure about it:
I have a sample code here
function redirect_page() {
if (isset($_SERVER['HTTPS']) &&
($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) ||
isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
$_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
$protocol = 'https://';
}
else {
$protocol = 'http://';
}
$currenturl = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$currenturl_relative = wp_make_link_relative($currenturl);
echo $currenturl_relative;
switch ($currenturl_relative) {
case '[from slug]':
$urlto = home_url('[to slug]');
break;
default:
return;
}
if ($currenturl != $urlto)
exit( wp_redirect( $urlto ) );
}
add_action( 'template_redirect', 'redirect_page' );
I am awaiting a response
Thank you in advance
You can use the PHP sleep() function to delay execution of a script.
Related
I've been using the code below for a while with no trouble. I redirect back to the main page if user is not an admin. I just installed SSL and now does not work anymore. I know it has something to do with the code checking for SSL but I'm not sure how to do that. Any help is appreciated.
function redirect_admin_login()
{
global $wpdb;
global $current_user;
$visitor = $current_user->ID;
$login_page = home_url('');
$page_viewed = basename($_SERVER['REQUEST_URI']);
if ($page_viewed == "wp-admin" && $_SERVER['REQUEST_METHOD'] == 'GET' && $visitor != '1')
{
wp_redirect($login_page);
exit;
}
}
add_action('init', 'redirect_admin_login');
Give the following code a shot
function admin_redirect()
{
if (!current_user_can('administrator') && (!defined('DOING_AJAX') || !DOING_AJAX ))
{
wp_safe_redirect(get_home_url());
exit();
}
}
add_action('admin_init', 'admin_redirect', 1);
I have a small bug in the construction of my URL, I've setup a test for when I am not using port:80 and for some reason if I use say port:8080 it is applying the port number twice for some reason in the code cant explain it.
public function get_full_url()
{
/** get $_SERVER **/
$server = self::get('SERVER');
$page_url = 'http';
if(isset($server['HTTPS']) and $server['HTTPS'] == 'on')
{
$page_url .= 's';
}
$site_domain = (isset($server['HTTP_HOST']) and trim($server['HTTP_HOST']) != '') ? $server['HTTP_HOST'] : $server['SERVER_NAME'];
$page_url .= '://';
if($server['SERVER_PORT'] != '80')
{
$page_url .= $site_domain.':'.$server['SERVER_PORT'].$server['REQUEST_URI'];
}
else
{
$page_url .= $site_domain.$server['REQUEST_URI'];
}
return $page_url;
}
$_SERVER['HTTP_HOST'] would contain port number as it is set in the Host: header
I am trying the last 5 pages the user viewed on my site to them in a sidebar. Here is the code I am working with:
function curPageURL() {
$pageURL = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? 'https://' : 'http://';
$pageURL .= ($_SERVER['SERVER_PORT'] != "80") ? $_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['REQUEST_URI'] : $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
return $pageURL;
}
$currentPage = curPageURL();
// $_SESSION['pages'] = $currentPage;
$_SESSION['pages'][] = $currentPage;
if (count($_SESSION['pages']) > 10) {
array_shift($_SESSION['pages']);
if (isset($_SESSION['pagehistory']) && count($_SESSION['pagehistory']) > 10) {
array_shift($_SESSION['pagehistory']);
echo '<h2>Page History</h2>
<ul>';
foreach ($_SESSION['pagehistory'] as $page) {
echo '<li>'.$page.'<li>';
}
echo '</ul>';
}
}
$_SESSION['pagehistory'][] = (!empty($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : '';
// var_dump($_SESSION); // enable this to show the $_SESSION-arrays made above
When I use this code though nothing appears on my page. So basically I would like to show the user the last 5 pages they have viewed on my site and also like to show the page name not the urls.
You could have a cookie that holds the names of recent pages.
Cookie array for Recently Viewed - need to extract data from array and cap cookie to 5 IDs
I am using below script to create sessions
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (
false !== strpos($url,'home') ||
false !== strpos($url,'display-job') ||
false !== strpos($url,'search-results-jobs') ||
false !== strpos($url,'find-jobs') ||
false !== strpos($url,'edit-profile1') ||
false !== strpos($url,'my-account/?myacount=1')
)
{
$_SESSION['page_name'] = 'jobseeker';
}
else {
$_SESSION['page_name'] = 'employer';
}
I can use the above script to check if someone is on one of the following sub pages but the problem is that i want to trigger a different session when someone is on the root of the webpage and I cant figure a way out.
try this
$url = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$root_url = $_SERVER['SERVER_NAME'];
$arr_url = explode("/", $url);
unset($arr_url[sizeof($arr_url)-1]);
$new_url = implode("/",$arr_url);
if($new_url==$root_url)
{
// do your work
}
I have this condition here:
if($_SERVER['REQUEST_URI'] != '/page.php' || ($_SERVER['REQUEST_URI'] != '/' && $_SERVER['REQUEST_URI'] != '/index.php')){
//do something, but not on index or page . php
}
it works on the index page, but not page.php...what am i doing wrong?
You can do it easier...
if( ! in_array( $_SERVER['REQUEST_URI'], array("/page.php", "/", "/index.php") ) ) {
// do something...
}
You have logical error use
if(!($_SERVER['REQUEST_URI'] == '/page.php' || $_SERVER['REQUEST_URI'] == '/' || $_SERVER['REQUEST_URI'] == '/index.php')){
//do something, but not on index or page . php
}
or better
if(!in_array($_SERVER['REQUEST_URI'], ["/page.php", "/", "/index.php"])){
//do something, but not on index or page . php
}