I´m actually using Codeigniter with php in my project and when a user is logged at the page if its a normal user load the same view if his a admin but changing some things in it.
I think that passing a variable to the controller and depending in it load the view with the changes depending on the user.. but the url its seems not ver cool let se this:
public function dc($q="")
{
if($q=="o")
{
// Here i have to change some parts of the template for normal users
$this->load->view('Main/template_main', $data, FALSE);
}elseif ($q=="a") {
// Here i have to change some parts of the template for admins
$this->load->view('Main/template_main', $data, FALSE);
The url is like www.xxx.com/controller/o or /a I want to see it like www.xxx.com/controller because its the index page..
Thanks for your time..
You should approach this in the way that Damien suggests. Your controller should set session data something like:
$this->session->set_userinfo('is_admin', FALSE);
Then, when you have checked the login creds, you can set the session data when the user is an admin:
$this->session->set_userinfo('is_admin', TRUE);
On your page, you can then set who sees what based on this value.
Related
My today goal is to make URL unavailable to other registered users if one of the user opened that URL and make it available when this user leaves it. Right now I have only idea how to block it - using cache, but problem is with unblocking as user can go everywhere or even just close the browser. I know that there is option for set cache timer, but in my case this is NOT an option - system must know if there is some user in that URL. Can someone help me with some ideas how to achieve this?
Only thing I can personally think of is using JWT or sessions to check if a logged in user is on that page, then a condition to check if the current user is not equal user that is trying to get on the page, and if it doesn't match just redirect that user to somewhere else.
export const blockAccess = expressAsyncHandler(async (req, res, next) => {
//check if the user is currently on the page
if (req.session.promoPage) {
//if they are, redirect new user's that're trying to access the page to the home page
if (req.session.promoPage.userId !== req.user.id) {
res.redirect('/')
}
}
next() //next here is a function that tells the program to move on to the next middleware function or route etc....
})
I have an action, /controller/edit, which can be clicked to from multiple locations (e.g. /controller/index and /controller/view).
Once the user has finished editing the record and clicks save, I want them to return to wherever they came from (/controller/index or /controller/view respectively).
At first I tried storing each page the user visits in their session (e.g. home > index > edit), and then redirecting to the second-to-last entry in that list - and that works fine right up until they open another tab. If, while they're editing the record they open another tab and go off somewhere else, their session variable keeps being built on (home > index > edit > help > help page), and when the time comes to redirect the second-to-last entry no longer contains the correct action.
How can my edit action /controller/edit, on save, redirect the user back to the referring page they came from to get there, independent of whatever the user has done in other tabs?
Edit: I can't use referrer after save because the process is this:
User is on either /controller/index or /controller/view
User clicks into /controller/edit (referrer is /controller/index or /controller/view)
User makes changes to record and clicks save, which form submits to /controller/edit (referrer is /controller/edit)
I now want to redirect them to either /controller/index or /controller/view, but that is no longer in the referrer
Use to return to wherever you came from
return $this->redirect($this->referer());
I had to deal with the same problem and I did this:
1) In my controller, in the GET section, set the referer to a view variable like this:
$this->set('redirect', $this->referer());
2) In the form, set a hidden control named "redirect":
echo $this->Form->hidden('redirect', ['value' => $redirect]);
3) In the controller, after processing the POST request, redirect to the value of the hidden input:
if (isset($this->request->data['redirect'])) {
$this->redirect($this->request->data['redirect']);
}
Of couse, this could be abstracted into a component and maybe use session, but this approach worked for me.
You can redirect to the referring page, using return $this->redirect($this->referer());
As per the Documentation it will redirect you to the referring page.
This is extension to #DanielCoturel answer. Step 1. in his answer is redundant (see my comment). I did it more globally by overwriting redirect() function in AppController.php (still have to add 'redirect' input to all forms):
public function redirect($url, $status = 302)
{
if ($this->request->getData('redirect', '')) {
return parent::redirect($this->request->getData('redirect'), $status);
}
return parent::redirect($url, $status);
}
For example, I have a page called profile_page.php. This page is only functional if data is written after the ?u= in the URL, for example, data for Alice's profile page can only be seen when the URL reads http://localhost/profile_page/alice.
Loading http://localhost/profile_page will give me undefined variable errors as most of my variable's are depending on the URL to have a value after the ?u=. For example, the variable $firstname can only be gathered when I get her username in the URL.
In such a case, when http://localhost/profile_page, I would rather have it redirect the user to their own profile_page, but I don't know how I can test the URL and parse it through an if statement.
I understand you can use $u=$_GET['u']; to obtain the current page URL? but I don't think doing this, is the best way to go about it:
$u=$_GET['u'];
if ($u == "http://localhost/profile_page/"){
// redirect to logged in users page code here
}
First, if you are using some parameter for your page to build, the url would looks like httlp://localhost/profile_page.php?firstname=alice&lastname=brown, with $_GET['firstname'] you will get alice in this case. If you want to test if the parameter is set first and redirect to another page if it is not set, you could use
if(!isset($_GET['firstname'])
{
header('Location:redirected_page.php');
}
Hello I am new to codeigniter. I am learning this framework by developing a testing application. I am showing user listing and in front of each record an anchor tag to edit that record. That anchor tag looks like
echo(anchor('first/edit_user/'.$value->id,'Edit'));
It redirects the browser to first controller and edit_user function. At edit_user function I load the view of edit like this.
$this->load->view('edit_user',$data);
It loads the view with respect to selected record to edit the record and the url looks like
http://localhost/CodeIgniter/index.php/first/edit_user/9
Every thing works fine. But when user clicks the update button it again comes to same function. It updates record and then again tries to load the same view but here a problem occurs. Now after updating record it loads the view and url becomes like this
http://localhost/CodeIgniter/index.php/first/edit_user
It creates error as it is not having the id of the selected record. IF i change the load view code of my function like this
$this->load->view('edit_user/'$this->uri->segment(3)),$data);
It generates an error of edit_user/.php is not defined and some thing like that. Now I want to ask How can I Redirect the user to same edit form telling that his record has been updated? How to load the view of
selected record from function of the controller?
To redirect to the same URL as the current one:
redirect(current_url());
Otherwise, be specific where you want to redirect.
$this->load->view('edit_user/'$this->uri->segment(3)),$data);
It generates an error of edit_user/.php is not defined and some thing like that.
Instead of using specific URL segments, use the parameters passed to your controller and set defaults, as well as handle the possible absence or invalidity of the arguments passed (url segments). Example:
function edit_user($id = NULL)
{
if ( ! $id) // handle error (redirect with message probably)
$user = $this->user_model->get($id);
if ( ! $user) // User not found, handle the error
// If user found, load the view and data
}
Remember that controllers are still just php classes and functions, and accept user input (anything I can type in the address bar) - so never assume anything about what's in the URL, always respond appropriately if the data you need isn't there.
First load the URL helper "config/autoload.php"
/*
| -------------------------------------------------------------------
| Auto-load Helper Files
| -------------------------------------------------------------------
| Prototype:
|
| $autoload['helper'] = array('url', 'file');
*/
$autoload['helper'] = array('url');
Or load the URL helper in your function itself
public function edit_user($user_id = '')
{
if(!is_numeric($user_id) {
// user id is not here redirect somewhere //
redirect('home/index');
}
if(isset($_POST['submit'])) {
// proceed to model // #param is optional // you can pass hidden field from form also //
$this->user_model->edit_user($user_id)
}
$this->load->helper('url');
$this->load->view('first/edit_user', $data);
}
Now go to your form :
<form method="post" action="<?php echo current_url(); ?>">
</form>
This will return to same edit page, after submit also.
Hope this helps you, let us know if anything there... Thanks!!
I have used Yii::app()->user->returnUrl but it always redirect me to localhost/index.php. Is there any particular configuration or some pieces of code that other programs that I must write? If you have another solutions let me know it.
#aslingga, can you please explain what you're trying to do with returnUrl? Are you just trying to get back to where you were after logging in, or are you using it somewhere else?
This is from the Yii documentation:
Redirects the user browser to the
login page. Before the redirection,
the current URL (if it's not an AJAX
url) will be kept in returnUrl so that
the user browser may be redirected
back to the current page after
successful login. Make sure you set
loginUrl so that the user browser can
be redirected to the specified login
URL after calling this method. After
calling this method, the current
request processing will be terminated.
In other words, if the page you're trying to request requires authentication, the URI of the page you're on gets stored in a session variable. Then, once you've logged in, it takes you back to that page.
One way I'd recommend troubleshooting is to do a print_r($_SESSION); just to make sure the returnUrl is actually being stored. Then you'll be able to check if index.php is being stored as returnUrl or if you're just being redirected there for some reason.
Looking at the CWebUser methods getState and setState might also be helpful.
I know this question is old but maybe this will help someone out since I didn't couldn't find a decent answer anywhere.
How getReturnUrl works
Setting a default return URL for your Yii app requires a bit of customization. The way it works out of the box is that you specify the default return URL each time you call it:
Yii::app()->user->getReturnUrl('site/internal');
The idea being that if a user were to visit a page that requires authentication, they will get redirected to the login page, but not before the site running
Yii::app()->user->setReturnUrl('site/visitedpage');
Now when the user logs in, they will be returned to the page they intended to go to.
While I like that functionality, having to set the default return URL each time is dumb. If you want to change the default return URL, you have to go find it throughout your code. I suppose you could set the value in a site parameter and call
Yii::app()->user->getReturnUrl(Yii::app()->params['defaultReturnUrl']);
I don't think I have to explain why that solution is annoying too.
My Solution
So when getReturnUrl is called without any parameters, it returns either '/index.php' or just '/'. This is fine in some cases, but not always. This is better IMO.
First, extend the CWebUser class and add the following extras
class WebUser extends CWebUser {
// default return URL property
public defaultReturnUrl;
// override the getReturnUrl method
public function getReturnUrl($defaultUrl=NULL) {
if ($defaultUrl === NULL) {
$defaultReturnUrl = $this->defaultReturnUrl;
}
else {
$defaultReturnUrl = CHtml::normalizeUrl($defaultUrl);
}
return $this->getState('__returnUrl',$defaultReturnUrl);
}
}
Now, let's add a couple items to the user component array.
'user' => array(
'class' => 'WebUser',
'defaultReturnUrl' => 'site/internal'
)
Not only does this allow you to set a default return URL in the config, but also maintains the ability to set a different default return URL and use the setReturnUrl functionality.
I think, you must set it:
Yii::app()->user->setReturnUrl('controller/action');