Wordpress too many redirect custom function - php

I tried to set custom redirects for specified files, like if url contains wp-login.php then redirect to the home url, the first statement is working and redirect the page to the home url but in the else i got this error:
ERR_TOO_MANY_REDIRECTS
Here is my code in functions.php
function secure_redirect(){
$page_viewed = basename($_SERVER['REQUEST_URI']);
if($page_viewed != "wp-login.php?secure_login")
{
if(strpos($page_viewed, "wp-login.php") !== false)
{
wp_redirect(get_site_url());
}
}
else
{
wp_redirect(get_site_url()."/".$page_viewed);
}
}
add_action('init','secure_redirect');

If you only want specific to redirect then why you have put redirection code in else part also? Skip the else part. Write redirection code only on if part
function secure_redirect(){
$page_viewed = basename($_SERVER['REQUEST_URI']);
if($page_viewed != "wp-login.php?secure_login")
{
if(strpos($page_viewed, "wp-login.php") !== false)
{
wp_redirect(get_site_url());
}
}
}
add_action('init','secure_redirect');

Related

change the redirection path after login

I want to change the redirection success page for all user in drupal 7.
my login page in mysite.com/user after successfull login its getting redirected to the page mysite.com/user/1.butr I want to change the login successful redirection page to
mysite.com/node/1
I have tried as
function bartik_user_login(&$edit, $account)
{
// Your logic will set $redirection to the desired location
$redirection = 'node/1';
// Unless there is already a redirection going, or the user is trying to reset his password, we redirect to $redirection.
if (empty($_GET['destination'])
&& !is_null($redirection)
&& (!isset($_POST['form_id']) || $_POST['form_id'] != 'user_pass_reset'))
{
$_GET['destination'] = $redirection; // Should we use $edit['redirect'] instead..?
}
}
Because the $_GET['destination'] always exists (it is user/{id} in your case), so your assignment $_GET['destination'] = $redirection will never be reached.
Just remove empty($_GET['destination'] from if condition and your code will work:
function bartik_user_login(&$edit, $account)
{
$redirection = 'node/1';
if (!is_null($redirection)
&& (!isset($_POST['form_id']) || $_POST['form_id'] != 'user_pass_reset'))
{
$_GET['destination'] = $redirection;
}
}

codeigniter session : php

I have one header file for the home page and the login page
in that header. I wrote this code on top of it
<?php
if ($this->session->userdata('logged_in')==true ){
redirect('home','refresh');
}else{
redirect('home/login','refresh');
}
?>
but it keeps redirecting me to the same page and it shows nothing
Remove the else redirect on login page, its already in login page
if ($this->session->userdata('logged_in')==true ){
redirect('home');
}
except login, you can redirect the page :)
to differ the login page and home page just send the
$this->data["pagename"]="login"; in login function and
$this->data["pagename"]="home"; in home function
and in header
if ($this->session->userdata('logged_in')==true ){
redirect('home');
} else {
if($pagename!="login")
{
redirect('home/login');
}
}
$user_logged_in=$this->session->userdata('logged_in');
if (!$user_logged_in)
{
redirect('home/login','refresh');
}else{
redirect('home','refresh');
}
<?php
if ($this->session->userdata('logged_in')!==FALSE){
redirect('home','refresh');
}
else{
redirect('home/login','refresh');
}
?>
try the above code..
Try this
put these line in every controller's __construct function
if ($this->session->userdata('logged_in') !=true) {
if ($this->router->fetch_class() != 'home' && $this->router->fetch_method() != "login") {
redirect("home/login");
}
}

Opencart closed login

I want to create an opencart webshop where customerts can see the content if they are logged in, otherwise redirect to a login page. I want to write it into the code somewhere, without any extension. I am really new in opencart.
Please add following code on catalog/controller/common/header.php inside index() function
if (!$this->customer->isLogged()) {
if (!isset($this->request->get['route']) || $this->request->get['route'] != 'account/login') {
$this->redirect($this->url->link('account/login', '', 'SSL'));
}
}
If you want to enable both register and login, then following:
if (!$this->customer->isLogged()) {
if (!isset($this->request->get['route']) || ($this->request->get['route'] != 'account/login' && $this->request->get['route'] != 'account/register')) {
$this->redirect($this->url->link('account/login', '', 'SSL'));
}
}
You need add below code
if (!$this->customer->isLogged()) { //content code here
}else{ $data['content']=''}
add above code on controller and enclose content so that only login user will be able to see that content.
and add link "login to view content" and use anchor tag to
login page
if($content=''){
//redirect code
}

Error on function php

I want go to the home pages as the privilege of user that current logged in.
When i am trying to open the page that having the link it goes to the home page. What changes that i need on my code
<body>
Home
</body
function home()
{
if($_SESSION['privillage']=="ADMIN")
{
header('location:admin_home.php');
}
elseif($_SESSION['privillage']=='SUPERVISOR')
{
header('location:home.php');
}
else
{
header('location:user_home.php');
}
}
Your function is being used inside an <a href="...">, so it's clearly supposed to return a URL (also you need to echo it)
Your current code is trying to redirect the user immediately, which won't work because you've already sent <a href=".
Try:
function home() {
if($_SESSION['privillage'] == "ADMIN") return "admin_home.php";
if($_SESSION['privillage'] == "SUPERVISOR") return "home.php";
return "user_home.php";
}
header('location is used if you want a PHP script to redirect the browser to another page. What you are trying to do is simply changing the href based on certain conditions.
function home()
{
if($_SESSION['privillage']=="ADMIN")
{
return 'admin_home.php';
}
elseif($_SESSION['privillage']=='SUPERVISOR')
{
return 'home.php';
}
else
{
return 'user_home.php';
}
}
For readability, you could consider using PHP's switch statement
may be you have to capitalize the first letter of "location" use "Location" in header('Location:user_home.php'); :)

Redirect into SilverStripe's pages

I'm trying to set up a redirect on my SilverStripe site.
I have created my own module, and now I want the user to be redirected to that after log in.
I've tried with Director::redirect($Url_base . 'myModule'), but it doesn't work.
Does anyone have any suggestions?
Try this: http://www.ssbits.com/customize-the-redirect-after-a-successful-member-login/
I did something like this:
class MyLoginForm extends MemberLoginForm {
public function dologin($data) {
parent::dologin($data);
if (Director::redirected_to() && $Member = Member::currentUser()) {
$this->controller->response->removeHeader('Location');
if ($Member->Email == 'admin') {
$destination = '/admin';
} else {
$destination = '/user/' . $Member->Username;
}
Director::redirect($destination);
}
}
}
If it is the admin user I redirect them to /admin. If it is another user I redirect them to /user/username.

Categories