codeigniter pass parameter in index($parameter) - php

I have to pass parameter through index, The the parameter of the index is used for user acess,
in my web site I gave each user different URL(www.domain.com/username)
as the user enter url it get to login page;
Now the url is www.domain.com/user_Method/username
its working fine , I want it as www.domain.com/username;
function user_Method($name)//LOGIN of store users According to privilage;
{
if($name=="")//if empty Url
{
redirect($this->config->base_url()."Error");
}
else if($name)
{
$data['storeid']=$this->admin_lib->get_where_id();//Get Store details
if($data['storeid']!="")
{
$this->load->view('login',$data);
}
else
{
redirect($this->config->base_url()."Error");
}
}
}

Set the base_url in config file and assign username to a variable then redirect to Base URL appended with username.
$config['base_url'] = 'http://www.example.com/';
$var = username;
redirect(base_url().$var);

When you want to pass a username or a id you may need to configure route
On the routes.php file Examples
$route['default_controller'] = 'test';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['(([^/]+)/?)'] = 'test/$1';
$route['error'] = 'test/error';
$route['(([^/]+)/?)'] = 'test/user_Method/$1';
User Guide http://www.codeigniter.com/user_guide/general/routing.html
Working image
<?php
class Test extends CI_Controller {
function index($somename = null) {
$somename = 'example';
if($somename == "") {
redirect(base_url() . 'error');
} else {
redirect(base_url() . $somename);
}
}
function user_Method($somename = NULL) {
echo "Working ";
echo $this->uri->segment(1);
}
public function error() {
echo "ERROR PAGE";
}
}
Url
http://localhost/codeigniter-project/username
With index.php
http://localhost/codeigniter-project/index.php/username
Config Base URL
$config['base_url'] = 'http://www.domain.com/';

Related

How to redirect a file using function

I'm trying to access my file where it contains a home page using a function from another file (redirect.php). MyWebsite/main/main.html this is the location of my file that I want to access. I stuck in here redirect::toMain('main.html'); don't have a idea how to link the full path so I will be directing to the home page. I tried to pass ('main.html') value to the function toMain() but it didn't work. Location of folder below.
So MyWebsite is the main folder and it contains sub-folder listed below.
function (folder) contains a redirect function
main (folder) contains a html. main.html where it set as my home page. This is where the user will redirect.
Class: redirect.php Location: MyWebsite/function/redirect.php
<?php
class redirect
{
public static function toMain($location = null)
{
if($location)
{
header('Location: ' . $location);
exit();
}
}
}
$a = new redirect();
?>
loginProcess.php
<?php
session_start();
require_once('../connection/connection.php');
require_once('../connection/loginCRUD.php');
require_once('../function/redirect.php');
if(isset($_POST['submit']))
{
$dbusername = $_POST['loginUsername']; //Store value of textfield in array
$dbpassword = $_POST['loginPassword'];
if(!empty($dbusername) && !empty($dbpassword))
{
if((new loginCRUD)->readLogin($dbusername,$dbpassword)) //If true
{
// echo "You are logged in!";
$_SESSION['loginUsername'] = $dbusername;//Session are stored in array.
// redirect the user to the home page
redirect::toMain('main.html');
}
else
{
echo "Incorrect username and/or password";
}
}
}//end of isset
?>
Here is one way that I would do this:
class redirect
{
private static $baseUrl = 'http://localhost/MyWebsite/main/';
public static function toMain($location = null)
{
if($location)
{
header('Location: ' . self::$baseUrl . $location);
exit();
}
}
}
The $baseUrl can also be this:
private static $baseUrl = '/MyWebsite/main/';
Make sure that MyWebsite starts with http:// or it will redirect to a relative URL. You don't need $a = new redirect(); since you are accessing the methods statically.
Now you can do this:
redirect::toMain('main.html');

Kohana/PHP Auth fails while issusing a request through AJAX

I have a base class which is inherited by all the controllers. I am having a function in the base class which determines the logged in users role using Auth. Once the users role is determine a variable $LoggedIn_role is set.
This method is correctly called on the initial page load, but later i am issuing ajax calls to check whether the user is still logged in, at that time the Auth::logged_in() always returning 0.
The kohana version i am using is 3.3
Can any one please suggest what is the best approach to circumvent this issue. Thanks.
To login -
if ($ValidObj->check()) {
if (Auth::instance()->login($_POST['email'], $_POST['password'],FALSE)) {
$this->DoPostLoginJobs();
} else {
$this->ManageError(Controller_Application::MsgWrongUserCredentials);
}
} else {
$this->Form_Errors = $ValidObj->errors('');
}
To Logout -
public function action_logout() {
$loggedout = Auth::instance()->logout();
if ($loggedout)
HTTP::redirect ('/home/'); // redirects to the home page.
}
Inside the controller_Application . The base class of all the controllers
public function DetermineUserRole() {
$this->LoggedIn_Role = Controller_Application::None;
try {
if (Auth::instance()->logged_in('freelancer')) {
$this->LoggedIn_Role = Controller_Application::Freelancer;
$this->LoggedIn_Id = Auth::instance()->get_user()->pk();
} else if (Auth::instance()->logged_in('employer')) {
$this->LoggedIn_Role = Controller_Application::Employer;
$this->LoggedIn_Id = Auth::instance()->get_user()->pk();
}
} catch (Gettrix_Exception $exc) {
$this->ManageError(Controller_Application::RedirectNonRecoverableError);
}
public function before() {
if ($this->request->is_ajax()) {
$this->auto_render = false;
}
$this->DetermineUserRole();
if($this->auto_render==TRUE){
parent::before();
$this->template->content = '';
$this->template->styles = array();
$this->template->scripts = array();
View::set_global('site_name', 'TheWebTeam');
View::bind_global('Form_Errors', $this->Form_Errors);
View::bind_global('LoggedIn_Role', $this->LoggedIn_Role);
View::bind_global('LoggedIn_Id', $this->LoggedIn_Id);
View::bind_global('InvitedEmail', $this->InvitedEmail);
View::bind_global('InvitedUniqueID', $this->InvitedUniqueID);
View::bind_global('scripts', $this->template->scripts);
View::bind_global('styles', $this->template->styles);
}
//This is inside the Home page controller, where it lists all the jobs for the logged in user.
public function action_joblist()
{
echo Auth::instance()->logged_in() . //The state holds to the initial state, doesn't //change when the user is logged out or logged in.
}
Please note that action_joblist() is called via AJAX/Jquery call.
The issue is fixed by following the instructions given in the link : http://forum.kohanaframework.org/discussion/9619/session-timeout-corruption-problems/p1

How to use two views in one controller in codeigniter

How to use two views in one controller in codeigniter
public function myaccount($user_id) {
$this->load->model('blog');
if(isset($_POST['post'])){
if(strlen($_FILES['inputUpProfile']['name']) > 0)
{
$pic = $this->do_upload('inputUpProfile');
if ($this->input->post('post') == ''){$type="image";} else {$type="image-with-text";}
}
else {$pic = ""; $type = "text"; }
$result = $this->blog->addPost($user_id, $type , $this->input->post('post'),$pic);
}
if(isset($_SESSION['user_id']) || !empty($_SESSION['user_id'])){
$result = $this->blog->getPost($user_id, 0 , 10);
$this->template->build("profile" , array("response"=>$result));
}
else{
$this->template->build('registration_view',$this->data);
}
$this->data['user'] = $user;
$this->data['errors'] = $this->errors;
$this->template->set_layout('myaccount');
$this->template->build('profile',$this->data);
$this->template->build('post_profile',$this->data);
}
}
this ic controller function that must open two view, but my problem is open one view.
Try like
$this->data['myaccount'] = $this->template->set_layout('myaccount');
$this->data['profile'] = $this->template->build('profile',$this->data);
$this->template->build('post_profile',$this->data);
And you can access the two other views from myaccount and profile
Or in your view file call the other 2 views like in post_profile page try like
$this->template->build('profile',$profile);
That $profile will be taken from the controller asigned to the post_profile view

Controller help and functions

I have a general create function that submits a new user to the database - this works fine. Where I am is stuck is the following.
I know that I need the user that is signing up to have clicked the link in the email before the account can login. How do I implement that into my if statement when I run the create function?
I am a bit confused as to how to set my errors if any thing is correct or wrong to do with the activation process I have currently set the messages using $this->form_validation->set_message();. Do I need to use set_flashdata();? and how will echo these into the view?
When I create a new user I have the userActive field set at 0 by default and also the default group is set to users
Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users extends CI_Controller {
public function index()
{
$data['companyName'] = $this->core_model->companyDetails()->coreCompanyName;
$data['pageTitle'] = "Create User";
$this->load->view('frontend/assets/header', $data);
$this->load->view('frontend/users', $data);
$this->load->view('frontend/assets/footer');
}
public function create(){
//If form validation fails load previous page with errors else do the job and insert data into db
if($this->form_validation->run('createUser') == FALSE)
{
$data['success'] = "";
}else{
$username = $this->input->post('userName');
$password = $this->input->post('userPassword');
$firstname = $this->input->post('userFirstName');
$lastname = $this->input->post('userLastName');
$email = $this->input->post('userEmail');
$passwordHash = $this->encrypt->sha1($password); // Lets encrypt the password why sha1? MD5 is for tossers
$activateCode = $this->_activateCode(10);
// If the data is correct follow through with db insert
if($this->users_model->createUser($username,$passwordHash,$firstname,$lastname,$email,$activateCode))
{
$data['success'] = TRUE;
redirect('frontend/users/create','refresh');
}
}
$data['companyName'] = $this->core_model->companyDetails()->coreCompanyName;
$data['pageTitle'] = "Create User";
$this->load->view('frontend/assets/header', $data);
$this->load->view('frontend/user_create', $data);
$this->load->view('admin/assets/footer');
echo get_class($this);
var_dump(method_exists($this, '_activateCode'));
}
function _userRegEmail($activateCode,$email,$firstname,$lastname){
$data['companyName'] = $this->core_model->companyDetails()->coreCompanyName;
$data['companyEmail'] = $this->core_model->companyDetails()->coreCompanyEmail;
$data['companyContact'] = $this->core_model->companyDetails()->coreContactName;
$data['firstName'] = $firstName;
$data['lastName'] = $lastname;
$data['email'] = $email;
$data['activateCode'] = $activateCode;
$this->email->from($this->core_model->companyDetails()->coreCompanyEmail, $this->core_model->companyDetails()->coreCompanyName);
$this->email->to($email);
$this->email->subject($this->core_model->companyDetails()->coreCompanyName, 'User Registration Confirmation');
$messageContent= $this->load->view('email_templates/userReg','', TRUE);
$this->email->message($messageContent);
//$this->email->send();
}
function usersconfirm(){
$activateCode = $this->uri->segment(3);
if($activateCode == '')
{
$this->form_validation->set_message('userConfirmError', 'Sorry you did not have a correct Activation Code.');
}
$userConfirmed = $this->users_model->confirm_user($activateCode);
if($userConfirmed){
$this->form_validation->set_message('userConfirmed', 'Thanks your account is now active you may login!');
}else{
$this->form_validation->set_message('userRecord', 'I am sorry we do not have any details with that Activation Code');
}
}
function _username_check($username)
{
if($this->users_model->username_taken($username))
{
$this->form_validation->set_message('username_check', 'Sorry the username %s is taken!');
return FALSE;
}else{
return TRUE;
}
}
function _email_check($email)
{
if($this->users_model->email_check($email))
{
$this->form_validation->set_message('email_check','Sorry there is already a user with this %s');
return FALSE;
}else{
return TRUE;
}
}
function _activateCode($length)
{
return random_string('alnum', $length);
}
}
/* End of file users.php */
/* Location: ./application/controllers/users.php */
You can determine if the user has clicked the activation link by checking the database for userActive in your if statement.
You can use flash data, sure. You can retrieve flash data with:
$this->session->flashdata('item'); to echo out to the view.
See http://codeigniter.com/user_guide/libraries/sessions.html > flash data

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