I have created a simple register/login system and it (I think) works well so far.
Now I want to implement some of it's functions to my site.
controllers/main.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends CI_Controller
{
public function index()
{
$this->home();
}
public function home()
{
$this->load->view('home.php');
}
}
views/home.php:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Home</title>
</head>
<body>
<div id="container">
<?php if ($this->session->userdata('is_logged_in')): ?>
logged in as: <?php echo $this->session->userdata('username'); ?></br>
Logout
<?php else: ?>
not logged in</br></br>
<?php echo form_open('login'); ?>
<?php echo form_error('username'); ?>
<input type="text" name="username" placeholder="Login" />
</br>
<?php echo form_error('password'); ?>
<input type="password" name="password" placeholder="Password" />
</br></br>
<div><input type="submit" value="Sign in" /></div>
<?php echo form_error('login_validation'); ?>
</form>
<?php endif ?>
</div>
</body>
</html>
Is this the right way of doing things in CI?
For controller part instead of using the home(), you can directly load the view in index() because internally you are navigating from index to home() without any specific reason.
Also the loading view doesn't require ".php" extension in your file and you can do like this:
$this->load->view('home');
Where home.php is the file located inside application/views/
For showing the error messages, you can use another optimal way by defining the validation rules(in array fromat) inside config/form_validation.php and just require below code to show all messages instead one by one
<?php echo validation_errors()?>
Related
I am working on a simple quiz web app and i want to validate each and every question on submit button showing the correct and add marks for right questions.
I have created my first question page like this
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Cric Quiz </title>
<link href="<?php echo base_url(); ?>assets/style.CSS" rel="stylesheet" type="text/css"/>
</head>
<body>
<div>
<h1>Cric Quiz</h1>
</div>
<div id='quesNo'>
<center>Question 01</center>
</div>
<div id='questions'>
<center>When did Australia win their first ever world cup?</center>
</div>
<div id='answers'>
<input type="radio" name="year" value="1992">1992<br>
<input type="radio" name="year" value="1996">1996<br>
<input type="radio" name="year" value="2000">2000<br>
<input type="radio" name="year" value="2012">2012
</div>
<center>Next</center>
</body>
</html>
My controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Questions extends CI_Controller {
public function question_1()
{
$this->load->view('quiz_one');
}
public function question_2()
{
$this->load->view('quiz_two');
}
}
How to validate my answers in my controller
PS: I'm using codeigniter
Your approach is wrong. Just have one function and pass the question id to that. If tomorrow, you have 1000 questions, will you create as many functions??
First store the correct answer in database.
Next your html should have the forms, submit the form with post data
Post the question id in form post
Add this code in controller,
// run query to fetch data from database based on the question in
if($this->input->post("year") == "value from database"){
} else {
}
I made a new application with just 2 views in application/views and 1 controller. I just want to go from page1 in view to page2 in view via the controller.
here is the whole code:
https://www.dropbox.com/s/cytt5azgerrbsg2/Afstudeerproject.zip?dl=0
Relevant Code
view 1:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Welcome to CodeIgniter</title>
</head>
<body>
<div id="container">
<h1>Welcome to CodeIgniter!</h1>
<div id="body">
<?= form_open(site_url('Welcome/steven')) ?>
<input type="file" name="userfile" />
<p><input type="submit" value="submit" name="submit" /></p>
<?= form_close() ?>
</div>
</div>
</body>
</html>
view 2:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Welcome to CodeIgniter</title>
</head>
<body>
<div id="container">
<h1>Welcome to CodeIgniter!</h1>
<div id="body">
<?= form_open(site_url('welcome')) ?>
<input type="file" name="userfile" />
<p><input type="submit" value="submit" name="submit" /></p>
<?= form_close() ?>
</div>
</div>
</body>
</html>
controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index() {
$this->load->view('welcome_message');
}
public function steven() {
$this->load->view('welcome_secondpage');
}
}
routes.php:
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
config.php:
$config['index_page'] = '';
$config['base_url'] = '';
autoload.php:
$autoload['helper'] = array('form' , 'url');
the rest of the files are all standard CodeIgnitor
In your config file assign this $config['index_page'] = 'index.php';
I am running into this problem with codeigniter, where it loads the index() page perfectly, but when it comes to any other function, it just throw me an 404 error. Here is a skeleton code :
<?php if( ! defined('BASEPATH')) exit('No direct script access alloowed');
Class User extends CI_Controller{
var $err;
public function construct(){
parent::_construct();
session_start();
$this->load->database();
$this->load->model('User_Model');
}
public function index(){
if(isset($this->session->userdata['userID'])){
$this->welcome();
}else{
$this->load->view('banner','Login');
if(isset($this->err)){
$data["error"] = $this->err;
$this->load->view('login',$data);
}else{
$this->load->view('login');
}
$this->load->view('footer');
}
}
public function test(){
echo "Hello World";
}
public function welcome(){
$this->load->view('test');
}
public function register(){
$this->load->view('banner','Login');
if(isset($this->err)){
$data["error"] = $this->err;
$this->load->view('register',$data);
}else{
$this->load->view('register');
}
$this->load->view('footer');
}
public function registration(){
//registration ops
}
public function login(){
//login ops
}
}
?>
So, when I am trying to navigate to www.example.com/index.php/user , it runs perfect, but when I try to let it run register, or do a login operation, it just throws an 404 error. Is there any solution to this?
Here is a html code generated using with index():
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="http://localhost/baseball/css/login.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div>
<p><img src="http://thumbs.dreamstime.com/z/cartoon-baseball-player-vector-illustration-30999087.jpg" height="250" alt=""/>Baseball Card Shop</p>
<hr />
<p class="loginInfo">
Shopping Cart
</p>
<hr/>
</div>
<div class="central_login">
<div class="main">
<form action="http://localhost/baseball/index.php/user/login" method="post" accept-charset="utf-8"> <div class="left">
<p>
<label for="login"> Login: </label>
<input type="text" name="login" id="login" pattern="^[a-zA-Z0-9]+$" required/>
</p>
<p>
<label for="pwd">Password: </label>
<input type="password" name="pwd" id="pwd" required/>
</p>
<p><label id='pwdErr'></label>
<input type="submit" value="Log in" id="create-account" class="button"/>
</p>
</div>
</form> <div class="right">
<form action="http://localhost/baseball/index.php/user/register" method="post" accept-charset="utf-8"> <p>Not our customer, worry not ! Register here, it is free!</p>
<input type="submit" value="Register HERE!" id="register" class="button"/>
</form> </div>
</div>
</div>
<footer>
<p>CSC309 Assignment</p>
<p>Baseball Web Application</p>
</footer>
</body>
</html>
Thanks a bunch!
You need to change your .htaccess. The following .htaccess will help you:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
And need to change the following configure in config.php:
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
There are a couple of things that are drawing attention in your code:
First, the use of var key word. It was used to declare class members in php4.
It is deprecated in php5 and greater version.
It will work. But raise a warning and is a bad practice.
Second. From the code you have posted, I believe, you have not auto loaded Session library.
Change the session_start() code in your constructor to :
$this->library->load('session');
Third thing I noted is this line of code:
$this->load->view('banner','Login');
The view functions of the loader class takes parameters in the below format.
$this->load->view('file_name', $data, true/false);
The syntax that you are might not be correct.
If you are planning to load banner and login file, you should do this:
$this->load->view('banner');
$this->load->view('Login');
On a side note, using native php sessions and CI sessions might be confusing and also may raise conflict in some situations.
i am tring to set cookie in codeigniter but i cannt make it happen i dont know what is wrong.
there is no error on the error log page...
this is my view page...
<!DOCTYPE html>
<html>
<head>
<title>
EKART
</title>
</head>
<body>
<h1>SIGN IN</h1>
<form id="admin" action="/do/ekart/adminlogin/login/" method="POST">
Name :<br/>
<input type="text" name="name" id="name"/><div id="name_display"></div><br/>
Password :<br/>
<input type="password" name="password" id="pasword"/><div id="password_display"></div><br/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
and here is my controller..
adminlogin.php
<?php
class adminlogin extends Controller{
public function __construct(){
parent::Controller();
$this->load->library('mylib');
}
public function index(){
$this->load->view("ekart/adminlogin");
}
public function login(){
$name=$_POST["name"];
$password=$_POST["password"];
$data=array("username"=>$name,"password"=>$password);
$result=$this->mylib->logincheck($data);
if($result){
echo "every thing is fine";
setcookie("myCookie",$name,time()+3600,"/");
$this->load->view("ekart/home",array("message"=>""));
}
}
}
the control is going inside if and it also prints "every thing is fine" the home page is also get loaded but i dont know why the cooke is still not set..
mylib is my library to check login validation....
There is a codeigniter way of doing this.
Make sure to have $this->load->helper('cookie'); in your controller, or autoload it.
$cookie = array(
"username" => $name,
"time" => time()+3600
);
$this->input->set_cookie($cookie);
I am trying to create a dynamic sidebar in zend framework. I have googled some articles, browsed even the stackoverflow archive but i can't seem to get it so please help me figure this out.
Here is some code from my layout.phtml file:
<div id="contentWrapper">
<div id="contentArea">
<?php echo $this->layout()->content;
?>
</div>
<div id="sidebar">
<div id="user-authentication">
<?php if (Zend_Auth::getInstance()->hasIdentity()) {
?>Logged In as<br />
<?php
echo Zend_Auth::getInstance()->getIdentity();
} else {
?>
<input type="text" name="login" class="loginInput" /><br/>
<input type="password" name="password" class="loginInput" /><br/>
<input type="submit" name="submit" value="Log In" class="loginButton" />
<?php } ?>
</div>
<div id="sidebar-content">
<? echo $this->layout()->sidebar; ?>
</div>
</div>
</div>
I could use this Best practice creating dynamic sidebar with zend framework but that means I would need to have redundant code for displaying the login box/logged in as.
Are you just worried about the repetition of hasIdentity and getIdentity in the sidebar-content div?
If so, maybe you'd prefer this:
<?php
$auth = Zend_Auth::getInstance();
$user = $auth->hasIdentity() ? $auth->getIdentity : NULL;
?>
<div id="user-authentication">
<?php if ($user) { ?>
User stuff.
<?php } else { ?>
Public stuff.
<?php } ?>
</div>
<div id="user-authentication">
<?php if ($user) { ?>
User stuff.
<?php } else { ?>
Public stuff.
<?php } ?>
</div>
However, that's just a matter of coding style. There's nothing wrong with checking a users logged in state more than once. It's necessary with modular code to do so; that's probably why Zend_Auth is a Singleton.
Looks like you need a widget:
Using Action Helpers To Implement Re-Usable Widgets - phly, boy, phly
For displaying or not some of the content, look for integration of Zend_Acl and Zend_Navigation.
It is as easy as: $container->setAcl($acl)