Redirecting back to referer - php

I am trying to get the referrer url passed as a variable so that I can link back to that page if a cookie isn't already set when a user lands on said page.
Currently looking into $_SERVER['HTTP_REFERER'] but not too sure how that works and how I would pass that in the below function.
Any help would be greatly appreciated.
function cookie_check() {
$post_ID = get_the_ID();
$parent_ID = wp_get_post_parent_id( $post_ID );
if($parent_ID == 9 || $post_ID == 9) {
if(!isset($_COOKIE["mycookie"])) {
header("Location: http://test.dev");
die();
}
}
if($parent_ID == 7 || $post_ID == 7) {
if(!isset($_COOKIE["diffcookie"])) {
header("Location: http://test.dev");
die();
}
}
}

Related

wp_redirect loop when using geoip

I'm getting an endless redirect loop that results in a too many redirects error when trying to redirect a user when detecting a specific country.
What I'm trying to achieve is to add to the end of the request url a param that is the language and redirect to the same page that will result in them viewing the site in their own language.
Here's what I've done:
add_action('template_redirect', 'geoip_redirect');
function geoip_redirect()
{
if (function_exists('geoip_detect2_get_info_from_current_ip')) {
$user_info = geoip_detect2_get_info_from_current_ip();
$country_code = $user_info->country->isoCode;
$request_url = $_SERVER['REQUEST_URI'].'?lang=he';
$url = get_site_url(null, $request_url);
if ($country_code == 'IL') {
wp_redirect($url);
exit();
}
}
}
You need to add a condition like checking for the lang parameter before executing the code otherwise It will keep executing forever.
add_action('template_redirect', 'geoip_redirect');
function geoip_redirect()
{
if (function_exists('geoip_detect2_get_info_from_current_ip')) {
if ( ! isset( $_GET['lang'] ) ) {
$user_info = geoip_detect2_get_info_from_current_ip();
$country_code = $user_info->country->isoCode;
$request_url = $_SERVER['REQUEST_URI'].'?lang=he';
$url = get_site_url(null, $request_url);
if ($country_code == 'IL') {
wp_redirect($url);
exit();
}
}
}
}

Redirect if user is not an admin is not working on SSL

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);

How to add Buy Now button in cs-cart

I'm creating a buy-now button cs-cart add-on. I've created an add-on and I've made the add-to-cart functionality work. But cant redirect it to checkout page. I have used "fn_redirect("checkout")" function for the redirection. And used this function below this:
"fn_add_product_to_cart($_REQUEST['product_data'], $cart, $auth);"
What should I do for redirection?
Edit:
My controller (buy_now.php)
if ($mode == 'add') {
if (empty($auth['user_id']) && Registry::get('settings.General.allow_anonymous_shopping') != 'allow_shopping')
{
return array(CONTROLLER_STATUS_REDIRECT, "auth.login_form?return_url=" . urlencode($_REQUEST['return_url']));
}
// Add to cart button was pressed for single product on advanced list
if (!empty($dispatch_extra)) {
if (empty($_REQUEST['product_data'][$dispatch_extra]['amount'])) {
$_REQUEST['product_data'][$dispatch_extra]['amount'] = 1;
}
foreach ($_REQUEST['product_data'] as $key => $data) {
if ($key != $dispatch_extra && $key != 'custom_files') {
unset($_REQUEST['product_data'][$key]);
}
}
}
$prev_cart_products = empty($cart['products']) ? array() : $cart['products'];
fn_add_product_to_cart($_REQUEST['product_data'], $cart, $auth);
fn_save_cart_content($cart, $auth['user_id']);
$previous_state = md5(serialize($cart['products']));
$cart['change_cart_products'] = true;
fn_calculate_cart_content($cart, $auth, 'S', true, 'F', true);
if (md5(serialize($cart['products'])) != $previous_state && empty($cart['skip_notification'])) {
$product_cnt = 0;
$added_products = array();
if (!empty($added_products)) {
Registry::get('view')->assign('added_products', $added_products);
if (Registry::get('config.tweaks.disable_dhtml') && Registry::get('config.tweaks.redirect_to_cart')) {
Registry::get('view')->assign('continue_url', (!empty($_REQUEST['redirect_url']) && empty($_REQUEST['appearance']['details_page'])) ? $_REQUEST['redirect_url'] : $_SESSION['continue_url']);
}
fn_redirect(Registry::get('config.https_location') . "/checkout");
// $msg = Registry::get('view')->fetch('views/checkout/components/product_notification.tpl');
//fn_set_notification('I', __($product_cnt > 1 ? 'products_added_to_cart' : 'product_added_to_cart'), $msg, 'I');
$cart['recalculate'] = true;
} else {
fn_set_notification('N', __('notice'), __('product_in_cart'));
}
}
unset($cart['skip_notification']);
$_suffix = '.checkout';
}
`
hook template : add_to_cart.post.tpl
{$id = "buy_now_{$product.product_id}"}
<button id="opener_{$id}" name="dispatch[buy_now.add..{$product.product_id}]" class=" vs-button buynow_btn_">Buy Now</button>
I'd consider putting your fn_redirect("checkout") code into an if/else statement to see what's happening with it.
If you have this code:
fn_add_product_to_cart($_REQUEST['product_data'], $cart, $auth);
Then surely you can put the redirection to the checkout page underneath it?
$redirect_url = "http://testcheckout.com?test=test"; //change this!
$errorMessage = "I should have redirected already....";
if (!empty($redirect_url)) {
//if it's not empty then it should redirect.
fn_redirect($redirect_url);
//Then put a die statement in here just to check it's not executing
//anything else and you can see if the redirect has a problem:
die(print_r($errorMessage, true ));
}
else {
//otherwise there is a problem with the supplied redirect url (empty)
die(print_r($redirect_url, true ));
}
I'd also take a look at this page which might help more:
http://forum.cs-cart.com/topic/6873-direct-buy-button/
Hope that helps!

If conditional with or

Here is my script:
$user_data = user_data($name);
if($user_data['type'] == 'clerk') {
} elseif ($user_data['type'] == 'admin' ){
}else
{ header("Location: login_admin1.php");}
}
If I am using an if statement with blank {} it works but as soon as I use this script it's not working.
if($user_data['type'] != 'clerk' || $user_data['type'] != 'admin')
{ header("Location: login_admin1.php");}
I want to check if $user_data['type'] is clerk or admin, (only clerk or admin has access to page) can check page. If it's not one of them they can't access the page.
Why not use in_array()?
if ( ! in_array( $user_data['type'], array( "clerk", "admin" ) ) ) {
// Is neither Clerk, nor Admin.
}
Cuts down on having to write $user_data['type'] over and over.
Use && instead of || in if statement:
Try:
if( $user_data['type'] != 'clerk' && $user_data['type'] != 'admin' ) {
header("Location: login_admin1.php");
}

Undefined index and unable to modify headers already sent [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
We're using a script to manage cookies on our joomla install. It works fine. When we install it on another site that isn't joomla we are facing these errors
Notice: Undefined index: prefcookie
Warning: Cannot modify header
information - headers already sent by (
Our code is
<?php
error_reporting(-1);
$url="/path/filter.php?u=http%3A%2F%2F".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
if ($_COOKIE['prefcookie'] == "path-all")
{
return;
}
elseif ($_COOKIE['prefcookie'] == "path-first")
header("Location: http://".$_SERVER["HTTP_HOST"]."$url%2F&b=2");
elseif($_COOKIE['prefcookie'] == "path-block")
header("Location: http://".$_SERVER["HTTP_HOST"]."$url%2F&b=2");
else
header("Location: http://".$_SERVER["HTTP_HOST"]."$url%2F&b=3");
?>
EDIT
That surpreses the error but doesn't solve the problem, the idea is that this checks the visitor for a preference and redirects the visitor to part of the site based on it, this fix just brings the visitor back to the requested file
The full code is
<?php
require_once('path/geoip/geoplugin.class.php');
$geoPlugin_array = unserialize( file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $_SERVER['REMOTE_ADDR']) );
if ( $geoPlugin_array['geoplugin_continentCode'] == 'EU' )
{
require_once("path/includes/browser.php");
$browser = new Browser();
if( $browser->getBrowser() == Browser::BROWSER_GOOGLEBOT )
{
return;
}
elseif( $browser->getBrowser() == Browser::BROWSER_SLURP )
{
return;
}
elseif( $browser->getBrowser() == Browser::BROWSER_MSNBOT )
{
return;
}
else
{
$url="/path/filter.php?u=http%3A%2F%2F".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
// This will avoid undefined index
if ((!isset($_COOKIE['prefcookie'])) || ($_COOKIE['prefcookie'] == "path-block")) {
return;
}
elseif($_COOKIE['prefcookie'] == "path-first")
header("Location: http://".$_SERVER["HTTP_HOST"]."$url%2F&b=2");
elseif($_COOKIE['prefcookie'] == "path-block")
header("Location: http://".$_SERVER["HTTP_HOST"]."$url%2F&b=2");
else
header("Location: http://".$_SERVER["HTTP_HOST"]."$url%2F&b=3");}
}
?>
EDIT
Set pref cookie
<?php
if($_GET['optin'] == "all")
{
setcookie("prefcookie", "path-all", time()+60*60*24*30);
header("Location: http://".$_SERVER["HTTP_HOST"]);
}
elseif($_GET['optin'] == "first")
{
setcookie("prefcookie", "path-first", time()+60*60*24*30);
header("Location: http://".$_SERVER["HTTP_HOST"]."/path/filter.php?u=http%3A%2F%2Fwww.fatcowmedia.com%2F&b=2");
}
elseif($_GET['optin'] == "block")
{
setcookie("prefcookie", "path-block", time()+60*60*24*30);
header("Location: http://".$_SERVER["HTTP_HOST"]."/path/filter.php?u=http%3A%2F%2Fwww.fatcowmedia.com%2F&b=3");
exit;
}
?>
EDIT
how to find requested url
$url="/path/filter.php?u=http%3A%2F%2F".$_SERVER["HTTP_HOST"].$_SERVER['REQUEST_URI'];
send to requested url
header("Location: http://".$_SERVER["HTTP_HOST"]."$url%2F&b=2");
EDIT
<?php
error_reporting(E_ALL);
$url="/path/filter.php?u=http%3A%2F%2F".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
require_once('path/geoip/geoplugin.class.php');
$geoPlugin_array = unserialize( file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $_SERVER['REMOTE_ADDR']) );
if ( $geoPlugin_array['geoplugin_continentCode'] == 'EU' )
{
require_once("path/includes/browser.php");
$browser = new Browser();
if( $browser->getBrowser() == Browser::BROWSER_GOOGLEBOT )
{
return;
}
elseif( $browser->getBrowser() == Browser::BROWSER_SLURP )
{
return;
}
elseif( $browser->getBrowser() == Browser::BROWSER_MSNBOT )
{
return;
}
else
{
// If expected cookie isn't set yet, send em to landing page
if (!isset($_COOKIE['prefcookie'])) {
header("Location: http://".$_SERVER["HTTP_HOST"]."$url%2F&b=2");
}
// If we made it this far, we have our expected cookie, we can implement a switch
switch ($_COOKIE['prefcookie']) {
case 'path-block':
case 'path-first':
header("Location: http://".$_SERVER["HTTP_HOST"]."$url%2F&b=2");
break;
default:
header("Location: http://".$_SERVER["HTTP_HOST"]."$url%2F&b=3");
break;
}
}
}
?>
UPDATED
==========
<?php
require_once('path/geoip/geoplugin.class.php');
$geoPlugin_array = unserialize( file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $_SERVER['REMOTE_ADDR']) );
if ( $geoPlugin_array['geoplugin_continentCode'] == 'EU' )
{
require_once("path/includes/browser.php");
$browser = new Browser();
if( $browser->getBrowser() == Browser::BROWSER_GOOGLEBOT )
{
return;
}
elseif( $browser->getBrowser() == Browser::BROWSER_SLURP )
{
return;
}
elseif( $browser->getBrowser() == Browser::BROWSER_MSNBOT )
{
return;
}
else
{
if (!isset($_COOKIE['prefcookie'])) {
header("Location: http://".$_SERVER["HTTP_HOST"]."/path/filter.php?u=http%3A%2F%2Fwww.path.org".$_SERVER["REQUEST_URI"]."&b=2");
}
elseif($_COOKIE['prefcookie'] == "path-all")
{
return;
}
elseif($_COOKIE['prefcookie'] == "path-first")
header("Location: http://".$_SERVER["HTTP_HOST"]."/path/filter.php?u=http%3A%2F%2Fwww.path.org".$_SERVER["REQUEST_URI"]."&b=2");
elseif($_COOKIE['prefcookie'] == "path-block")
header("Location: http://".$_SERVER["HTTP_HOST"]."/path/filter.php?u=http%3A%2F%2Fwww.path.org".$_SERVER["REQUEST_URI"]."&b=2");
else
header("Location: http://".$_SERVER["HTTP_HOST"]."/path/filter.php?u=http%3A%2F%2Fwww.path.org".$_SERVER["REQUEST_URI"]."&b=2");
}
}
?>
Should probably add a check to make sure that the element exists in the array:
// This will avoid undefined index
if ((!isset($_COOKIE['prefcookie'])) || ($_COOKIE['prefcookie'] == "path-all")) {
return;
}
-- Update --
According to your updates and comments, the following code should work:
// If expected cookie isn't set yet, send em to landing page
if (!isset($_COOKIE['prefcookie'])) {
header("Location: http://".$_SERVER["HTTP_HOST"]."$url%2F&b=2");
}
// If we made it this far, we have our expected cookie, we can implement a switch
switch ($_COOKIE['prefcookie']) {
case 'path-block':
case 'path-first':
header("Location: http://".$_SERVER["HTTP_HOST"]."$url%2F&b=2");
break;
case 'path-all':
header("Location: http://".$_SERVER["HTTP_HOST"]);
break;
default:
header("Location: http://".$_SERVER["HTTP_HOST"]."$url%2F&b=3");
break;
}
For the record, in your original code you had 'path-block' twice, so I assumed it to be the same as 'path-first' as you have it. If this is not desired, you can copy and past the header location for 'path-first' and paste it under 'path-block'.
Also, it appears your url construct is malformed:
$url="/path/filter.php?u=http%3A%2F%2F".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
// If you decode the following construct it looks like (I decoded entities for clarity)
// http://www.example.com/path/filter.php?u=http://www.example.com/path/filter.php/&b=2
// %2F decodes to a slash "/", so the /&b=2 appears malformed, it should be /?b=2
header("Location: http://".$_SERVER["HTTP_HOST"]."$url%2F&b=2");
A useful encode/decode tool can be found # meyerweb.com/eric/tools/dencoder
You're seeing this notice because the index "prefcookie" does not exist. In all likelihood, on your Joomla install something is setting $_COOKIE['prefcookie'], but your install not on Joomla doesn't have this index.
Furthermore, the warning you are seeing is because you are trying to change the header information after it's already been sent. You can suppress this, however it's probably better to just redirect the user prior to sending the rest of the headers.

Categories