I am making a website in Codeigniter. I have two portions one is admin other is the user. Now I want to send value to a user controller from the admin folder.
In the admin side, my baseurl is "localhost/website/admin" and in the user, it is "localhost/website".
Now I want to send a value from the admin to the "ABC" controller in the user
like "localhost/website/user/ABC/2". the method to do this is baseurl('user/ABC/2') but the baseurl in the admin side is "localhost/website/admin". So how can I do this?
$foo = "localhost/website/user/ABC/2";
$bar = str_replace("/admin","/user", $foo);
echo $bar;
Replace $foo with baseurl
Related
i'm beginner in php so i want to ask this question
I read about url in Codeigniter and know url is include controller name and then method name but i didn't know what's question mark means yet ?
for example
where form post data if the url after click on submit button is
http://localhost/code/forums/topic/25674318?addpost=Publish
i'm using Codigniter may i use this url to post submitted data to model i mean insert that data to database.
This is the symbol ? which starts the begining of query in url. And after that you can use & symbol
<?php echo $this->input->get('addpost');?>
http://www.codeigniter.com/user_guide/libraries/input.html?highlight=get
Or
<?php echo $_GET['addpost'];?>
http://php.net/manual/en/reserved.variables.get.php
And then should return Publish
The fist query in url good to have ? then any other query you can use &
Example: http://www.demo.com/example/?token=123456&something=test
$var1 = 123456;
$var2 = 'test';
site_url('example/'. '?token=' . $var1 . '&somthing=' . $var2);
Config
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-&=?';
The default JUser::setParam() method sets parameters only to the session. Is is possible somehow to store parameters not in the session, that they would be always available? I also found in users table field params, which stores some parameters for current user, but don't know how to add data there...
Have you tried to save the object after adding your param?
$user = JFactory::getUser();
$user->setParam($key, $value);
$user->save;
An easy and comfortable approach would be to use the popular Community Builder Extension which lets you define custom user fields in the administrator backend. It has an API to obtain the CB User Object and to write and read those fields which get stored in the DB as opposed to the session. Simple example without knowing your Joomla/CB version (works with Joomla 2.5 and CB 1.8) and the place (site, admin, external) where this should be executed (assuming your parameter is named customParam and the user id is 42):
cbimport( 'cb.field' );
$mosCbUser = CBUser::getUserDataInstance(42);
// read value
$customParameter = $mosCbUser->customParam;
// write value
$mosCbUser->customParam = 'newnew';
$mosCbUser->store();
/* write value to DB directly with optional boolean third parameter
specifing whether to trigger the user update plugins
like onBeforeUserUpdate or onAfterUserUpdate
*/
$mosCbUser->storeDatabaseValue('cb_adresse', 'new address', false);
be sure to be in CB plugin context, if not already the case, include e.g. like
global $_CB_framework, $_CB_database, $ueConfig;
$app = JFactory::getApplication();
include_once( $app->getCfg( 'absolute_path' ) . '/administrator/components/com_comprofiler/plugin.foundation.php' );
I am in the process of starting a project with CakePHP and I am trying to get an understanding for the login process, followed by creating a session that contains the login/user fields:
function login_php(){
// -=> Retrieve form fields "u" and "p" (Username and password):
$a = $this->params['form']['u']; //email address.
$b = $this->params['form']['p']; //password.
// -=> Query Retrieve User, match email and password fields:
// -=> Make users data available in the view:
$this->set('users', $this->User->find('all', array('conditions' => array('User.email =' => $a , 'User.password =' => $b))));
}
At this point I would now like to create a number of session variables from the users fields and set them in the session:
//Setting Session Variables:
$this -> Session -> write( "name", $users['User']['fname'] . " " . $users['User']['lname']);
//Retrieving Session Variables:
echo $this -> Session -> read("name");
But I am having trouble with this last part of setting the session variable.
QUESTION:
How do you set the session variables for the user?
Any help appreciated guys...
You are aware cake comes with an authentication module already in place, yes?
http://book.cakephp.org/view/1250/Authentication
I use Symfony 1.4.11. I have two tables "companies" and "ads". When user add new ad, he can connect ad with his company.Before it I check, if user have company, for example I have variable $has_company, if $has_company==1 - user has company, if $has_company==0 he has not company. If user want connect company with ad, he must check checkbox :-) So I want to validate checkbox, If user check checkbox, and he has not company,I want to show messages, that first he must create company.... Is it possible? Can I use sfValidatorBoolean ? If yes, how to validate variable has_company? Thank you!
I think you can create a method in myUser class to check if the current user has a company (if your models user and company are linked).
And then, you can pass the result of this method in option of your form.
For validation, you can use a callback validator : http://www.symfony-project.org/forms/1_4/en/B-Validators#chapter_b_sub_sfvalidatorcallback
you can use halt_on_error option like
$v = new sfValidatorAnd(
array(
new sfValidatorString(array('max_length' => 255)),
new sfValidatorEmail(),
),
array('halt_on_error' => true),
array('invalid' => 'The input value must be an email with less than 255 characters.')
);
can anyone give me an example on how to create Sessions and write data to it. I've seen syntax on how to write data to a session using write command. But how to create a session and retrieve the values in it.
In my application, I have two data, form_id and user_id that needs to be used in all the page requests. So how do I save it as a session variable and use it across the application?
EDIT
function register()
{
$userId=$this->User->registerUser($this->data);
$this->Session->write('User.UserId',$userId);
//echo "session".$this->Session->read('User.UserId');
$this->User->data=$this->data;
if (!$this->User->validates())
{
$this->Flash('Please enter valid inputs','/forms' );
return;
}
$this->Flash('User account created','/forms/homepage/'.$userId);
}
How to use the session variable 'User.UserId' instead of $userId in $this->Flash('User account created','/forms/homepage/'.$userId);
And can I use this variable in all my view files,because in all the page requests I also pass the userId?
EDIT 2
I have 2 controllers,user and form. I write the userid to a session variable in the users_controller. I have a view file called homepage.ctp,whose action is in the forms_controller. Now how can I use the session variable defined in the users_controller in the homepage? Sorry if I am asking silly questions. I went through the cakebook,but my doubts weren't cleared. I'm also trying trial and error method of coding,so please help me.
EDIT 3
I have a session variable 'uid' which is the user id in the home page action of a controller.
$this->Session->write('uid',$this->data['Form']['created_by']);
I need the same variable in the design action method of the same controller.
When I give
$uid=$this->Session->read('uid');
echo "uid: ".$uid;
the value is not echoed.
Can't I use the session variable in the same controller?
The bakery is your best friend:
http://book.cakephp.org/view/398/Methods
All your session read/writes belong in the controller:
$this->Session->write('Person.eyeColor', 'Green');
echo $this->Session->read('Person.eyeColor'); // Green
In cake php you can create session like this
$this->request->session()->write('user_id', 10);
and you can read session value like this
echo $this->request->session()->read('user_id');
Super Simple!
You don't have to write any code to create session, they are already built in. Then you just use the read and write sessions as mentioned above. Also see here for more details:
http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html
Used in Controllers
http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html
Used in Views
cakephp 4 example of session usage in controllers, views and cells
$session = $this->request->getSession();
$session->write('abc', 'apple');
echo $session->read('abc');
In this case it would be:
$this->Flash('User account created','/forms/homepage/'.$this->Session->read('User.UserId'));
and your second question is anwered by Jason Miy (http://api.cakephp.org/class/session-helper). You can simply use this in your view:
$userId = $session->read('User.UserId');
Reading the appropriate cookbook pages slowly and carefully usually helps a lot...
I found out the reason why the uid wasn't being echoed(edit 3 part of the question).
It is due to a silly mistake, had a white space after the end tag ?> in the controller. Now it is working fine.
when I have strange session behavior, and this help me.
MODEL:
function clearAllDBCache() {
$db =& ConnectionManager::getDataSource($this->useDbConfig);
$db->_queryCache = array();
}
`
Acess your Helper SessionHelper in lib/Cake/View/Helper/SessionHelper.php and add the method:
public function write($name = null) {
return CakeSession::write($name);
}