0

I'm trying to figure out how I can make this page redirect to a certain page after 5 seconds of the page loading ONLY when the if ($this->usersmodel->activateUser($userID, $registrationKey)) returns TRUE. I can't use jquery because the problem with that is what if it returns false then I don't want it to redirect.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Activate extends CI_Controller { public function __construct() { parent::__construct(); $this->load->model('users/usersmodel'); } public function index() { //Config Defaults Start $msgBoxMsgs = array();//msgType = dl, info, warn, note, msg $cssPageAddons = '';//If you have extra CSS for this view append it here $jsPageAddons = '<script src='.base_url().'assets/peach/js/validate/activate.js></script>';//If you have extra JS for this view append it here $metaAddons = '';//Sometimes there is a need for additional Meta Data such in the case of Facebook addon's $siteTitle = '';//alter only if you need something other than the default for this view. //Config Defaults Start //examples of how to use the message box system (css not included). //$msgBoxMsgs[] = array('msgType' => 'dl', 'theMsg' => 'This is a Blank Message Box...'); /**********************************************************Your Coding Logic Here, Start*/ $userID = $this->uri->segment(2); $registrationKey = $this->uri->segment(3); if ( ((!is_numeric($userID)) || (is_null($userID))) || ((is_null($registrationKey)) || (!preg_match('/^[A-Za-z0-9]+$/', $registrationKey))) ) { $bodyContent = $this->config->item('defaultTemplate') ."/404";//which view file } else { $registrationDetails = $this->usersmodel->getRegistrationDetails($userID); if (!is_null($registrationDetails)) { if ($this->usersmodel->activateUser($userID, $registrationKey)) { $message = 'User was activated successfully! You may now login!'; $bodyContent = $this->config->item('defaultTemplate') ."/usermanagement/forms/activate";//which view file } else { $message = 'User was not activated successfully! Please try again with the right credentials!'; $bodyContent = $this->config->item('defaultTemplate') ."/usermanagement/forms/activate";//which view file } $this->data['message'] = $message; } else { $bodyContent = $this->config->item('defaultTemplate') ."/404"; } } $bodyType = "full";//type of template /***********************************************************Your Coding Logic Here, End*/ //Double checks if any default variables have been changed, Start. //If msgBoxMsgs array has anything in it, if so displays it in view, else does nothing. if(count($msgBoxMsgs) !== 0) { $msgBoxes = $this->msgboxes->buildMsgBoxesOutput(array('display' => 'show', 'msgs' =>$msgBoxMsgs)); } else { $msgBoxes = array('display' => 'none'); } if($siteTitle == '') { $siteTitle = $this->metatags->SiteTitle(); //reads } //Double checks if any default variables have been changed, End. $this->data['msgBoxes'] = $msgBoxes; $this->data['cssPageAddons'] = $cssPageAddons;//if there is any additional CSS to add from above Variable this will send it to the view. $this->data['jsPageAddons'] = $jsPageAddons;//if there is any addictional JS to add from the above variable this will send it to the view. $this->data['siteTitle'] = $siteTitle;//defaults can be changed via models/metatags.php $this->data['bodyType'] = $bodyType; $this->data['bodyContent'] = $bodyContent; $this->load->view($this->config->item('defaultTemplate') .'/usermanagement/index', $this->data); } } /* End of file activate.php */ /* Location: ./application/controllers/activate.php */ 

3 Answers 3

4

2 ways.. one you can use jquery/javascript and post/get for something to be done via JavaScript where if when posting the item returns true then you do

setInterval(function(){window.location = 'http://urlYouWannaGoto.com/';}, 3000); 

the other if you wish to go the php only route and should work with codeigniter as well. Is pushing a header out if in your case its true.

header('Refresh: 3; url=http://urlYouWannaGoto.com/'); 

as far as the PHP route goes you gotta also make sure the header is output before anything else so it will actually work.

Sign up to request clarification or add additional context in comments.

2 Comments

well where woudl that header be because I tried it and it didn't work.
Though I bet a lot of people will argue with this as it doesnt follow suit with MVC logic, I'd say leave it in your controller. This way nothing from the view level gets in the way, and knowing your template scheme some, it'll be easier to leave it in the controller
0

it seems to me you can just test this condition for true... if ($this->usersmodel->activateUser($userID, $registrationKey)) returns TRUE...If TRUE, initialize a timer for 5 seconds IF FALSE return false...

Comments

0

You can probably accomplish that by setting a refresh header.

header('refresh:5; url=some_url')

But I don't like that solution.

Edit:

I just looked at the source of CodeIgniter redirect().

if ( ! function_exists('redirect')) { function redirect($uri = '', $method = 'location', $http_response_code = 302) { if ( ! preg_match('#^https?://#i', $uri)) { $uri = site_url($uri); } switch($method) { case 'refresh' : header("Refresh:0;url=".$uri); break; default : header("Location: ".$uri, TRUE, $http_response_code); break; } exit; } } 

In practice they do the following:

header("Refresh:0;url=".$uri); exit; 

So what about the following:

header("Refresh:5;url=".'your_path'); exit; 

3 Comments

I tried that because I saw it on a different question before I asked and it didn't reload.
Okey. It seems that you are using CodeIgniter. If I remember correctly they should have an function called redirect which can set a refresh header for you. See redirect() in codeigniter.com/user_guide/helpers/url_helper.html
yes, you can use the CI redirect, but there is no delayed version, once called it redirects. OP is seeking a delayed response.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.