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);
Related
I am using Codeigniter in My website and it works very well in all browsers in windows or phones except in snapchat.
When I am trying to submit the form I get the message:
message undefined index 'clientname'
message undefined index 'clientmessage'
<form action="<?php echo base_url(); ?>R/H/212F32" method="POST">
<input type="text" name="clientname" placeholder="name">
<textarea rows="5" name="clientmessage" placeholder="message"></textarea>
<input type="submit" value="send" />
</form>
I tried using an extra button with javascript to popup message alert of clientname and clientmessage content but nothing happens so I guess that there is something that happening in snapchat browser ?
Edited :
Controller 'T' That display the form
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class T extends MY_Controller {
public function __construct(){
parent::__construct();
}
function _remap($param) {
$this->index($param);
}
public function index($param)
{
$data = array();
$this->load->view('Head');
$data['code'] = $param;
$this->load->view('NewForm' , $data);
$this->load->view('Foot');
}
}
Controller 'R' That recive the form submited
<?php
class R extends MY_Controller {
public function H($c){
$this->load->view('Head');
$text = $_POST['clientname'];
$message = $_POST['clientmessage'];
//$this->input->post wont work also
// $this->input->post('clientname');
// $this->input->post('clientmessage');
$this->load->view('Foot');
}
}
UPDATED 10/05/2019 :
I tried to do basic form submit like this :
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Snapchat issue !</title>
<meta name="description" content="">
<meta name="author" content="">
</head>
<body>
<form action="./Test.php" method = "post">
First name:<br>
<input type="text" name="firstname" value="Turki">
<br>
Last name:<br>
<input type="text" name="lastname" value="Test123">
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Test.php
<?php
var_dump($_POST);
?>
The result :
in Android:
array(2) { ["firstname"]=> string(5) "Turki" ["lastname"]=> string(7) "Test123" }
in IOS:
array(0) {}
same thing happen without codeigniter so I rolled it out , but I noticed the following :
1 - it work fine in Snapchat in Android , but the issue appear in IOS only
2- in IOS if the link was send using snapchat chat it will work fine too even in IOS , but if it was linked in Story the issue appear !!!!
I am following a mvc tutorial but i dont understand how and why my code does't work.
So i have a Controller like so.
public function indexAction()
{
$formSent = false;
if (isset($_POST['send'])){
$formSent = true;
}
$this->view->setVars([
'name' => 'Stefan',
'formSent' => $formSent
]);
}
and my form which is located in views/index
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Forumlar</title>
</head>
<body>
<form method="post" action="/index">
<table>
<tr>
<td>Vorname</td>
<td><input type="text" name="vorname"></td>
</tr>
<tr>
<td>Nachname</td>
<td><input type="text" name="nachname"></td>
</tr>
<tr>
<td>PLZ</td>
<td><input type="number" name="plz"></td>
</tr>
</table>
<button type="submit" name="send">Send</button>
</form>
</body>
<?php
echo $name;
if ($formSent){
echo "Form is Sent!";
}
?>
To make my question more simple to understand and where my problem is.
So echoing $name, does output in this case "Stefan".
While anything that is done with the form doesn not work.
For example dumping _POST will be empty and my if formSent statement does not work.
So how exacltey do i "connect" these two or how does it work ?
Thank you.
EDIT: Here its waht it says after i send the form
Object not found! The requested URL was not found on this server. The
link on the referring page seems to be wrong or outdated. Please
inform the author of that page about the error.
If you think this is a server error, please contact the webmaster.
Error 404
EDIT 2:
Yes i do thave this in my IndexController
protected $view;
public function setView(\Mvc\Library\View $view)
{
$this->view = $view;
}
To connect the view to controller, u need to load the view page in controller..
in controller,
public function index() {
$this->load->view('YOUR PAGE NAME IN VIEW FOLDER');
}
My login page is redirecting me again to my login page and don't know why. I think there have been some issues with my redirect syntax and I'm not able to resolve it so I need your expert help.
My code is below.
Controller file :
Verifylogin_controller.php
<?php
// if(!isset($_SESSION))
// {
session_start();
// }
class Homelogin_controller extends CI_Controller
{
public
function __construct()
{
parent::__construct();
}
function index()
{
if ($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_In');
$data['username'] = $session_data['username'];
$this->load->view('Home_view', $data);
}
else
{
redirect('Login_controller', 'refresh');
}
}
function logout()
{
$this->session->unset_userdata('logged_in');
session_destroy();
redirect('Homelogin_controller', 'refresh');
}
}
?>
Model file:
Login_model.php
<?php
class Login_model extends CI_Model
{
function login($username, $password)
{
$this->db->where('username', $username);
$this->db->where('password', $password);
$query = $this->db->get('login');
if ($query->num_rows() > 0)
{
return true;
}
else
{
return false;
}
}
}
?>
View file
login_view.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Login with codeigniter</title>
</head>
<body>
<h1>Simple Login with CodeIgniter</h1>
<?php echo validation_errors(); ?>
<?php echo form_open(base_url().'Verifylogin_controller'); ?>
<label for="username">Username:</label>
<input type="text" size="20" id="username" name="username"/>
<span><?php echo form_error ('username'); ?> </span>
<br/>
<label for="password">Password:</label>
<input type="password" size="20" id="passowrd" name="password"/>
<span><?php echo form_error ('password'); ?> </span>
<br/>
<input type="submit" value="Login"/>
http://localhost/codeIgniter/CodeIgniter-3.1.4/Verifylogin_controller/login_validation
<?php
$this->session->flashdata('error');
?>
</form>
</body>
</html>
Home_view.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Simple Login with CodeIgniter - Private Area</title>
</head>
<body>
<h1>Home</h1>
<h2>Welcome <?php echo $this->session->userdata('username') ?>!</h2>
Logout
</body>
</html>
EDIT: Moved my original answer to a comment, as it was correctly pointed out that it should have been.
You're never setting the $_SESSION['logged_In'] boolean. You're returning TRUE, but you're not actually setting anything. In your login controller, you should set that to true prior to returning anything. This is probably where your redirect problem is actually coming from. That may have been mentioned in the comments, I can't see them from this screen.
Here is my approach. There are some things wrong with it and some things that require some extra explaining. But here's my code:
$seg = $this->uri->segment(2);
if (($seg == "login") && (isset($uid)) && (ctype_digit($uid)) && (($uid != "0"))) {
redirect(base_url());
} elseif (($seg != "login") && ((!isset($uid)) || (!ctype_digit($uid)))) {
redirect(base_url() . 'index.php/user/login');
}
You need to test that the user is logged in AND check whether they're already on the login page. In my case, URI segment 2 is the controller. If they're already logged in AND they're on the login page, it redirects them to the base URL.
If they're NOT logged in and they're on any other page except the login page, it redirects them to the login page.
You don't need to set the $seg variable. I do it because I use it in other places and I don't want to have to type out the helper code again.
I use the Stencil template system, which is why I can use $uid instead of $_SESSION['uid']. I've modified the template code to pass all of the session variables to the view pages. Since you aren't using Stencil, you'll have to figure out where to put this code. I have it in the layout file, so I don't have to check for login on every single page.
Comments on your code
Don't call session_start() directly. Load the session library autoload config, if you want to use the CI session library. There are some issues with it, so you may want to look for an alternative or roll your own. This would prevent you having to call the session on every page.
You appear to be storing passwords in plain text, if this is a direct paste of your code. You should hash the password (password_hash) and store it in the database. Then hash the user's input and compare it with the database (password_verify).
You're setting user data with $session_data = $this->session->userdata('logged_In') then trying to access $session_data['username'], but you didn't actually store the username. You stored the "logged_In" bool.
I'm trying to execute a PHP class after a HTML form submit but my browser display that it couldn't open the specify address.
Here's my form:
<!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' xml:lang='en' lang='en'>
<head>
<title>Logins</title>
</head>
<body>
<div id='login_form'>
<form action='<?php echo base_url();?>Login/process' method='post' name='process'>
<h2>User Login</h2>
<br />
<label for='username'>UsernameTest</label>
<input type='text' name='username' id='username' size='25' /><br />
<label for='password'>Password</label>
<input type='password' name='password' id='password' size='25' /><br />
<input type='Submit' value='Login' />
</form>
</div>
</body>
</html>
And here's my PHP's class:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller{
function __construct(){
parent::__construct();
}
public function index(){
// Load our view to be displayed
// to the user
$this->load->view('login_view');
}
public function process(){
// Load the model
$this->load->model('login_model');
// Validate the user can login
$result = $this->login_model->validate();
// Now we verify the result
if(! $result){
// If user did not validate, then show them login page again
$this->index();
}else{
// If user did validate,
// Send them to members area
redirect('home');
}
}
}
?>
The class is in the Controller folder and the form is in the View folder
Thank's
If you configured your site as something like http://www.mywebsite.org in your config file, then the line:
<form action='<?php echo base_url();?>Login/process' method='post' name='process'>
Will render as:
<form action='http://www.mywebsite.orgLogin/process' method='post' name='process'>
which is obviously not what you intended because http://www.mywebsite.orgLogin/process isn't even a valid URL. To render URLs in CodeIgniter, use site_url():
<form action='<?php echo site_url('Login/process');?>' method='post' name='process'>
Aside for this, there may be two other problems you might want to check out:
the /Login/process page doesn't exist
the /home page doesn't exist
First echo or dump your base_url() to see what is there, or use site_url() .I usually use '/' as /controller_name/function and please use 'login' not 'Login'.
It works when I write manually the following URL:
localhost:8888/CodeIgniter-3.0.0/index.php/Login/process
But doesn't work using the form redirection.
<form action='<?php echo site_url('Login/process/');?>' method='post' name='process'>
Thanks
Code for myform.php
<html>
<head>
<title>My form</title>
</head>
<body>
<form action="success" method="post">
<h5>Username</h5>
<input type="text" name="username" value="" size="50" />
<!--
<h5>Password</h5>
<input type="password" name="password" value="" size="50" />
<h5>Confirm Password </h5>
<input type="password" name="passconf" value="" size="50" />-->
<h5>Email Address </h5>
<input type="email" name="email" value="" size="50" />
<div><input type="submit" value="submit" /></div>
</form>
</body>
</html>
controller script 1
<?php
class Form extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->library('session');
$this->load->helper('url');
}
public function index(){
$this->load->view('myform');
}
public function success(){
$_SESSION['username']=$_POST['username'];
$_SESSION['email']=$_POST['email'];
redirect('form/home');
}
public function home(){
$this->load->view('test_home');
}
}
?>
controller script 2
<?php
class Form extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->library('session');
$this->load->helper('url');
}
public function index(){
$this->load->view('myform');
}
public function success(){
$_SESSION['username']=$_POST['username'];
$_SESSION['email']=$_POST['email'];
echo $_SESSION['username'];
echo $_SESSION['email'];
redirect('form/home');
}
public function home(){
$this->load->view('test_home');
}
}
?>
The question is when I use controller 1, the script work as intended and redirects me to form/home. However, when I use controller 2 I get this error
A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at /usr/local/apache2/htdocs/parth/application/controllers/Form.php:15)
Filename: helpers/url_helper.php
Line Number: 564
Backtrace:
File: /usr/local/apache2/htdocs/parth/application/controllers/Form.php
Line: 18
Function: redirect
File: /usr/local/apache2/htdocs/parth/index.php
Line: 292
Function: require_once
why is the code behaving this way? Thank you for your time.
Its because, you have echoed two strings in second case.
Comment out this:
//echo $_SESSION['username'];
//echo $_SESSION['email'];
Redirection is not happening due to this.
In CodeIgniter, redirection uses PHP's header("Location...") construct.
This requires that your current script is not outputting anything on screen.
Not even a space (that is why CodeIgniter recommends you should not end up your PHP files with ?> as spaces can remain there.
redirect() uses PHPs
header() function. If you have output before any header you get this error.
This is your Output:
echo $_SESSION['username'];
echo $_SESSION['email'];
redirect('form/home');
Commented out, or delete it, cause while redirecting you dont need it.
Kind regards