Magento how to check if user is signed in? - magento

How do I check if a user is signed in?
Like:
<?php unless user_is_signed_in
echo "Please log in"
end
?>

Mage::getSingleton('customer/session')->isLoggedIn();
or
Mage::helper('customer')->isLoggedIn();
The second method calls the first - see here
So using the helper method as an example...
$isLoggedIn = Mage::helper('customer')->isLoggedIn();
if (! $isLoggedIn) {
echo "Please log in";
}

User the customer helper:
$this->helper('customer')->isLoggedIn()

Related

How can we redirect to previous page after login in cibonfire?

How can we redirect to previous page after login in cibonfire?
If some module require login to view, than the code there will check for login and if it is not logged in, it will redirect to login page. After login we want to get back to same module. How can we do so.
Thanks
Before your checking ,A user is already loged in ,Add
$this->session->set_userdata('page_url', current_url());
Add in your login function ,After checking the login data.
if($this->session->userdata('page_url'))
redirect($this->session->userdata('page_url'));
else
redirect('default home page');
At the end of your controller function add this line
redirect('your_view_here', $any_variables_here);
I use sessions for logging in and for navegation tasks like that, because it just makes life a lot simpler. In my controller functions I drop in a line of code like the following:
$this->load_segs(func_get_args(), strtolower(__CLASS__).'/'.__FUNCTION__);
Here's the load segements function:
function load_segs($params, $src){
if ( ! is_array($params) || ! isset($src)){
return FALSE;
}
foreach ($params as $key => $value){
//only include values that don't need encoding
if ($value != urlencode($value)){
break;
}
$src .= '/'.htmlentities($value, ENT_QUOTES, 'UTF-8');
}
$this->session->set_userdata('return', $src);
return $src;
}
You can retrieve the information as follows:
$return = html_entity_decode($this->session->userdata('return'));
redirect($return);

In yii how to insert record in table by overriding existing entry [duplicate]

In yii i am creating project. After validation of user's entered email, i am displaying password.php file which is having textfield for entering new password.
Password.php=
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'email-form',
'enableClientValidation'=>true,
));
echo CHtml::textField('Enter new password');
echo CHtml::textField('Repeat password');
echo CHtml::submitButton('Submit');
$this->endWidget();
When user will enter new password and click on submit button i want to insert this new password into User table's password field, in such a way that it overright old password.
In controller i had created method as-
public function actionCreate(){
if(isset($_POST['email']))
{
$record=User2::model()->find(array(
'select'=>'userId, securityQuestionId, primaryEmail',
'condition'=>'primaryEmail=:email',
'params'=>array(':email'=>$_POST['email']))
);
if($record===null) {
echo "Email invalid";
}
else {
echo "email exists";
$this->render('Password');
if(isset($_POST['Password']))
{
$command = Yii::app()->db->createCommand();
$command->insert('User', array(
'password'=>$_POST['password'] ,
//'params'=>array(':password'=>$_POST['password'])
}
}
}
else{
$this->render('emailForm'); //show the view with the password field
}
But its not inserting new password. So How can i implement this...Please help me
First of all the way you are handling the form is certainly not Yii-ish which means it's not really the way to go.
The way you should handle this is by creating an object which extends from the CFormModel and put all your logic code in there instead of in the controller.
Now, if you want to continue working with your piece of code, then it would be best to place the following piece of code
$this->render('Password');
BELOW the if isset password stuff.
For your problem, the reason why your password isn't being updated is because the query you created is not being executed. If we take a look here then we can see that the following piece of code should be added:
$command->execute();
Which will execute your piece of sql.
Something like this...
$user = User::find('email = :email', ':email' => $_POST['email']);
if( empty($user) )
return;
$user->password = $_POST['password'];
$user->save();
You can't get password like this $_POST['Password'], because you haven't set this post variable.
You had to use:
echo CHtml::textField('password');
echo CHtml::textField('repeatPassword');
echo CHtml::hiddenField('email', $email);
'password' and 'repeatPassword' are names of POST vars
And in your controller you have too many mistakes, try this (check for typos):
if(isset($_POST['email']))
{
$record=User2::model()->find(array(
'select'=>'userId, securityQuestionId, primaryEmail',
'condition'=>'primaryEmail=:email',
'params'=>array(':email'=>$_POST['email']))
);
if($record===null) {
echo "Email invalid";
}
else {
if(isset($_POST['password']) && isset($_POST['repeatPassword']) && ($_POST['password'] === $_POST['repeatPassword']))
{
$record->password = $_POST['password'];
if ($record->save()) {
$this->render('saved');
}
}
$this->render('Password' array('email'=>$_POST['email']));
}
}
}
else{
$this->render('emailForm'); //show the view with the password field
}
In your code if(isset($_POST['Password'])) won't ever execute, because after sending password you haven't set email variable. So you just $this->render('emailForm');. Thus we set it by CHtml::hiddenField('email', $email);
Upd. I strongly recommend you to read this guide. It will save a lot of time for you.

cakephp flash error not working

i've created a login in system where an activated user can log in but a user who's account hasnt been activated wont be able to log in. The problem is that no flash message is outputted when the user tries to log in with a deactivated account, so a user wouldn't know why the page keeps refreshing on the login page.
heres my login function
if ($this->request->is('post')){
if ($this->request->data['User']['password'] == 'qazwsx'){
if ($this->Auth->login()){
$username = $this->request->data['User']['username'];
if (0 === $this->User->find('count',array('conditions'=>array('activated'=>1,'username'=> $username)))) {
$this->Session->setFlash('Sorry, your account is not validated yet.');
$this->redirect($this->referer());
}
$this->Auth->user('id');
$this->redirect($this->Auth->redirect('eboxs/home'));
}
}
else {
$this->Session->setFlash('Username or password is incorrect');
}
}else{
$this->Session->setFlash('Welcome, please login');
}
}
I havent included my view cause so far with flash messages i havent had to call them
Weird Authentification way you're doing there.
Like you've been already told in the comments, check, wherever the user is redirected ($this->redirect), there is a
<?php echo $this->Session->flash(); ?>
in that view.
Also with that method, the user is logged in anyway. You've to manually log out that user again, before you show him the flash message / redirect him.
You can also just write:
<?php
...
$this->Auth->scope = array('User.activated' => 1);
if ($this->Auth->login()) {
// logged-in logic
} else {
// not logged-in logic
}
?>
This would save you some lines of code.

How to implement login and register?

I have two forms: (login form, register form) and they submitted to my component.
Login form:
I want to check login and pass and set status to "logged" in joomla.
Register form:
I want register new user in joomla.
How to do it?
$inplogin = JRequest::getVar('inplogin');
$inppass = JRequest::getVar('inppass');
???? JFactory::login($inplogin, $inppass); ????
???? JFactory::registeruser($inplogin, $inppass); ????
???
For Login
$result = JFactory::getApplication()->login(array('username'=>$username, 'password'=>$password));
For Registration
require_once JPATH_ROOT.DS.'components'.DS.'com_users'.DS.'models'.DS.'registration.php';
$model = new UsersModelRegistration();
jimport('joomla.mail.helper');
// Attempt to save the data.
jimport('joomla.user.helper');
$username = ; //your user name here
$password = ; //your password here
$data = array( 'username'=>$email,'name'=>$email,'email1'=>$email,'password1'=>$password, 'password2'=>$password, 'block'=>1 );
$return = $model->register($data);
I don't see how this is related specifically to joomla.
you need to use the same $_SESSION variable in both cases. after you declare the variable, you can check on the beginning of your index page if this var exists (and if so, whether the credentials are correct or not). something like:
<?php
session_start();
if (!isset($_SESSION['logged']) {
header("location:login.php");
exit();
}
?>
you can use JUser::bind($userData) and JUser::save($userData) for registering the user.
Expanding upon Gaurav's answer:
If you're using just the Joomla framework and you don't have a site loaded, you will still need to initialize the site.
That said, these two lines should do the trick:
$app = JFactory::getApplication('site');
$app->initialise();

Phpbb3 forum integration with existing site

I am trying to integrate the phpbb forum with my existing site. I have already looked at these links, and it doesn't seem to work. I have copied this code
define('IN_PHPBB', true);
define('ROOT_PATH', "/path/to/forums");
if (!defined('IN_PHPBB') || !defined('ROOT_PATH')) {
exit();
}
$phpEx = "php";
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : ROOT_PATH . '/';
include($phpbb_root_path . 'common.' . $phpEx);
$user->session_begin();
$auth->acl($user->data);
into a loginforum.php file, which I include in every page I want the sessions to be kept. I have done the three steps indicated in the sessions integration section, but when I try to check whether the user is authenticated, it doesn't seem so. Using the same code here:
<?php
if ($user->data['user_id'] == ANONYMOUS){
echo 'Please login!';
}
else{
echo 'Thanks for logging in, ' . $user->data['username_clean'];
}
?>
I only get the "Please login" phrase, even when I login.
I've been over this for hours, I don't understand where the problem is. Shouldn't it work after the three miraculous steps?
Try this:
if ($user->data['username'] == 'Anonymous')
{
echo 'Please login!';
}
This is the first (and guest) user in the PHPBB database:
SELECT `user_id`,
`username`,
`username_clean`
FROM
`phpbb_users` WHERE user_id = 1
Result:
"user_id" "username" "username_clean"
"1" "Anonymous" "anonymous"

Resources