unexpected end of file [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
I had an unexpected end of file error but I checked my code and I didn't find any sign of this error! So can you tell me what may be the cause of this problem! Here is my file's code that contains the error (this is a project done with cakephp):
<?php
App::uses('AppController', 'Controller');
class AdminsController extends AppController {
public function login($id = null)
{
$this->layout='login';
if ($this->request->is('post')) {
$user=$this->request->data['Admin']['r'];
if ($user == 'Administrator')
{
$Admin=$this->request->data['Admin']['login'];
$mdp=$this->request->data['Admin']['motpasse'];
$Admins=$this->Admin->find('count',array('conditions'=>array('Admin.login'=>$Admin,'Admin.motpasse'=>$mdp)));
if ($Admins ==1)
{sleep(2);
CakeSession::write('admin','admin' );
CakeSession::write('nom',$Admin);
$this->redirect(array('action' => 'index'));
}else {?><script type="text/javascript">
confirm('M.Admin Login ou Mot de passe incorrect');
</script><?}
}
if ($user == 'Commercial')
{
App::Import('Model', 'Commercial');
$Admin=$this->request->data['Admin']['login'];
$mdp=$this->request->data['Admin']['motpasse'];
$category = new Commercial();
$categories = $category->find('count',array('conditions'=>array('Commercial.login'=>$Admin,'Commercial.motpasse'=>$mdp)));
if ($categories ==1)
{
CakeSession::write('admin','com' );
CakeSession::write('nom',$Admin);
$this->redirect(array('controller' => 'Commercials', 'action' => ''));
}
}
}
}
public function logout($id = null)
{
$this->Session->destroy();
$this->redirect(array('action' => 'login'));
}
public function index() {
$admin=CakeSession::read('admin');
if(($admin<>'admin')&&($admin<>'com'))
{$this->redirect(array('controller' => 'Admin', 'action' => 'login'));}
if($admin=='com')
{$this->layout='Commercials';}
$this->Admin->recursive = 0;
$this->set('admins', $this->paginate());
}
public function view($id = null) {
$admin=CakeSession::read('admin');
if(($admin<>'admin')&&($admin<>'com'))
{$this->redirect(array('controller' => 'Admin', 'action' => 'login'));}
if($admin=='com')
{$this->layout='Commercials';}
if (!$this->Admin->exists($id)) {
throw new NotFoundException(__('Invalid admin'));
}
$options = array('conditions' => array('Admin.' . $this->Admin->primaryKey => $id));
$this->set('admin', $this->Admin->find('first', $options));
}
public function add() {
$admin=CakeSession::read('admin');
if(($admin<>'admin')&&($admin<>'com'))
{$this->redirect(array('controller' => 'Admin', 'action' => 'login'));}
if($admin=='com')
{$this->layout='Commercials';}
if ($this->request->is('post')) {
$this->Admin->create();
if ($this->Admin->save($this->request->data)) {
$this->Session->setFlash(__('The admin has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The admin could not be saved. Please, try again.'));
}
}
$questionnaires = $this->Admin->Questionnaire->find('list');
$this->set(compact('questionnaires'));
}
public function delete($id = null) {
$admin=CakeSession::read('admin');
if(($admin<>'admin')&&($admin<>'com'))
{$this->redirect(array('controller' => 'Admin', 'action' => 'login'));}
if($admin=='com')
{$this->layout='Commercials';}
$this->Admin->id = $id;
if (!$this->Admin->exists()) {
throw new NotFoundException(__('Invalid admin'));
}
$this->request->onlyAllow('post', 'delete');
if ($this->Admin->delete()) {
$this->Session->setFlash(__('Admin deleted'));
$this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Admin was not deleted'));
$this->redirect(array('action' => 'index'));
}
}
?>
This is the error that shows up:

And have a look here:
if ($Admins ==1)
{sleep(2);
CakeSession::write('admin','admin' );
CakeSession::write('nom',$Admin);
$this->redirect(array('action' => 'index'));
}else {?><script type="text/javascript">
confirm('M.Admin Login ou Mot de passe incorrect');
</script><? } // look at here
?>
that should be:
if ($Admins ==1)
{sleep(2);
CakeSession::write('admin','admin' );
CakeSession::write('nom',$Admin);
$this->redirect(array('action' => 'index'));
}else {?><script type="text/javascript">
confirm('M.Admin Login ou Mot de passe incorrect');
</script><?php } // your little mistake was here
?>
Did you notice you were missing php there.

Related

How to keep ID Model in another Model page in Cakephp 2

I have two tables Location and Car. What I want is, when I click on the picture of the car (View/Cars/view.ctp), redirect to the location add form (View/Locations/add.ctp) while keeping the ID of the car I've previously chosen.
LocationsController:
<?php
App::uses('AppController', 'Controller');
class LocationsController extends AppController {
public $components = array('Paginator', 'Session');
public $helpers = array(
'Js',
'GoogleMap'
);
public function index() {
$this->Location->recursive = 0;
$this->set('locations', $this->Paginator->paginate());
}
public function view($id = null) {
if (!$this->Location->exists($id)) {
throw new NotFoundException(__('Invalid location'));
}
$options = array('conditions' => array('Location.' . $this->Location->primaryKey => $id));
$this->set('location', $this->Location->find('first', $options));
}
public function add($car_id) {
if ($this->request->is('post')) {
$this->Location->create();
$this->set('car_id', $car_id);
if ($this->Location->save($this->request->data)) {
$this->Session->setFlash(__('The location has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The location could not be saved. Please, try again.'));
}
}
$users = $this->Location->User->find('list');
$agencies = $this->Location->Agency->find('list');
$cars = $this->Location->Car->find('list');
$this->set(compact('users', 'agencies', 'cars'));
}
public function edit($id = null) {
if (!$this->Location->exists($id)) {
throw new NotFoundException(__('Invalid location'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->Location->save($this->request->data)) {
$this->Session->setFlash(__('The location has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The location could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('Location.' . $this->Location->primaryKey => $id));
$this->request->data = $this->Location->find('first', $options);
}
$users = $this->Location->User->find('list');
$agencies = $this->Location->Agency->find('list');
$cars = $this->Location->Car->find('list');
$this->set(compact('users', 'agencies', 'cars'));
}
public function delete($id = null) {
$this->Location->id = $id;
if (!$this->Location->exists()) {
throw new NotFoundException(__('Invalid location'));
}
$this->request->allowMethod('post', 'delete');
if ($this->Location->delete()) {
$this->Session->setFlash(__('The location has been deleted.'));
} else {
$this->Session->setFlash(__('The location could not be deleted. Please, try again.'));
}
return $this->redirect(array('action' => 'index'));
}
public function admin_index() {
$this->Location->recursive = 0;
$this->set('locations', $this->Paginator->paginate());
}
public function admin_view($id = null) {
if (!$this->Location->exists($id)) {
throw new NotFoundException(__('Invalid location'));
}
$options = array('conditions' => array('Location.' . $this->Location->primaryKey => $id));
$this->set('location', $this->Location->find('first', $options));
}
public function admin_add() {
if ($this->request->is('post')) {
$this->Location->create();
if ($this->Location->save($this->request->data)) {
$this->Session->setFlash(__('The location has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The location could not be saved. Please, try again.'));
}
}
$users = $this->Location->User->find('list');
$agencies = $this->Location->Agency->find('list');
$cars = $this->Location->Car->find('list');
$this->set(compact('users', 'agencies', 'cars'));
}
public function admin_edit($id = null) {
if (!$this->Location->exists($id)) {
throw new NotFoundException(__('Invalid location'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->Location->save($this->request->data)) {
$this->Session->setFlash(__('The location has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The location could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('Location.' . $this->Location->primaryKey => $id));
$this->request->data = $this->Location->find('first', $options);
}
$users = $this->Location->User->find('list');
$agencies = $this->Location->Agency->find('list');
$cars = $this->Location->Car->find('list');
$this->set(compact('users', 'agencies', 'cars'));
}
public function admin_delete($id = null) {
$this->Location->id = $id;
if (!$this->Location->exists()) {
throw new NotFoundException(__('Invalid location'));
}
$this->request->allowMethod('post', 'delete');
if ($this->Location->delete()) {
$this->Session->setFlash(__('The location has been deleted.'));
} else {
$this->Session->setFlash(__('The location could not be deleted. Please, try again.'));
}
return $this->redirect(array('action' => 'index'));
}}
and this CarsController
<?php
App::uses('AppController', 'Controller');
class CarsController extends AppController {
public $components = array('Paginator', 'Session');
public $helpers = array('Js', 'GoogleMap');
public function admin_index() {
$this->Car->recursive = 0;
$this->set('cars', $this->Paginator->paginate());
}
public function view($id = null){
if (!$this->Car->exists($id)) {
throw new NotFoundException(__('Invalid car'));
}
$options = array('conditions' => array('Car.' . $this->Car->primaryKey => $id));
$this->set('car', $this->Car->find('first', $options));
}
public function admin_view($id = null) {
if (!$this->Car->exists($id)) {
throw new NotFoundException(__('Invalid car'));
}
$options = array('conditions' => array('Car.' . $this->Car->primaryKey => $id));
$this->set('car', $this->Car->find('first', $options));
}
public function admin_add() {
if ($this->request->is('post')) {
$this->Car->create();
if ($this->Car->save($this->request->data)) {
$this->Session->setFlash(__('The car has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The car could not be saved. Please, try again.'));
}
}
$categories = $this->Car->Category->find('list');
$subcategories = $this->Car->Subcategory->find('list');
$this->set(compact('categories', 'subcategories'));
$this->set('categories', $this->Car->Subcategory->Category->find('list'));
}
public function admin_edit($id = null) {
if (!$this->Car->exists($id)) {
throw new NotFoundException(__('Invalid car'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->Car->save($this->request->data)) {
$this->Session->setFlash(__('The car has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The car could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('Car.' . $this->Car->primaryKey => $id));
$this->request->data = $this->Car->find('first', $options);
}
$categories = $this->Car->Category->find('list');
$subcategories = $this->Car->Subcategory->find('list');
$this->set(compact('categories', 'subcategories'));
}
public function admin_delete($id = null) {
$this->Car->id = $id;
if (!$this->Car->exists()) {
throw new NotFoundException(__('Invalid car'));
}
$this->request->allowMethod('post', 'delete');
if ($this->Car->delete()) {
$this->Session->setFlash(__('The car has been deleted.'));
} else {
$this->Session->setFlash(__('The car could not be deleted. Please, try again.'));
}
return $this->redirect(array('action' => 'index'));
}
public function index() {
$this->set('cars', $this->Car->find('all'));
}
}
and this is cars/view.ctp :
<div class="cars view">
<h2><?php echo __('Car'); ?></h2>
<?php
echo $this->Html->input("cars/car_id", array(
"alt" => "Cars",
'url' => array('controller' => 'locations', 'action' => 'add', 'car_id')
));
?>
<dl>
<dt><?php echo __('Id'); ?></dt>
<dd>
<?php echo h($car['Car']['id']); ?>
</dd>
<dt><?php echo __('Title'); ?></dt>
<dd>
<?php echo h($car['Car']['title']); ?>
</dd>
<dt><?php echo __('Category'); ?></dt>
<dd>
<?php echo $this->Html->link($car['Category']['name'], array('controller' => 'categories', 'action' => 'view', $car['Category']['id'])); ?>
</dd>
<dt><?php echo __('Subcategory'); ?></dt>
<dd>
<?php echo $this->Html->link($car['Subcategory']['name'], array('controller' => 'subcategories', 'action' => 'view', $car['Subcategory']['id'])); ?>
</dd>
<dt><?php echo __('Color'); ?></dt>
<dd>
<?php echo h($car['Car']['color']); ?>
</dd>
<dt><?php echo __('Serial'); ?></dt>
<dd>
<?php echo h($car['Car']['serial']); ?>
</dd>
<dt><?php echo __('Model'); ?></dt>
<dd>
<?php echo h($car['Car']['model']); ?>
</dd>
<dt><?php echo __('Price'); ?></dt>
<dd>
<?php echo h($car['Car']['price']); ?>
</dd>
</dl>
</div>
<h5><?php echo $this->Html->link(__('Rent a Car'), array('controller'=>'locations','action' => 'add')); ?></h5>
and this locations/add.ctp :
<div class="locations form">
<?php echo $this->Form->create('Location'); ?>
<fieldset>
<legend><?php echo __('Add Location'); ?></legend>
<?php
echo $this->Form->input('status');
echo $this->Form->input('departure_date');
echo $this->Form->input('expected_return_date');
echo $this->Form->input('user_id');
echo $this->Form->input('agency_id');
echo $this->Form->input('car_id');
//echo $this->$Session->read('Auth.User.username');
//echo $this->$Session->read('Auth.Car.id');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('List Locations'), array('action' => 'index')); ?></li>
<li><?php echo $this->Html->link(__('List Users'), array('controller' => 'users', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('New User'), array('controller' => 'users', 'action' => 'add')); ?> </li>
<li><?php echo $this->Html->link(__('List Agencies'), array('controller' => 'agencies', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('New Agency'), array('controller' => 'agencies', 'action' => 'add')); ?> </li>
<li><?php echo $this->Html->link(__('List Cars'), array('controller' => 'cars', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('New Car'), array('controller' => 'cars', 'action' => 'add')); ?> </li>
</ul>
</div>
Assuming you used the answer to the earlier question you posted you will have the car_id set correctly. All you now need to do is add it to your form in add.ctp correctly.
Replace:
echo $this->Form->input('car_id');
With
echo $this->Form->input('car_id', array('type'=>'hidden', 'value'=>$car_id));
Your form will then save the car_id correctly.

cakephp : go to previous page after editing a player

I have players in pages. I'm for instance on page 13. Here I click on the edit function to edit a player. Now after the edit I want to get back to that page 13 but It stays at the edit page.
edit action :
public function admin_edit($id = null) {
if (!$this->Player->exists($id)) {
throw new NotFoundException(__('Invalid player'));
}
if ($this->request->is(array('post', 'put'))) {
$data = $this->request->data['Player'];
if(!$data['player_image']['name']){
unset($data['player_image']);
}
if ($this->Player->save($data)) {
$this->Session->setFlash(__('The player has been saved.'));
$this->redirect($this->referer());
} else {
$this->Session->setFlash(__('The player could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('Player.' . $this->Player->primaryKey => $id));
$this->request->data = $this->Player->find('first', $options);
}
$videos = $this->Player->Video->find('list');
$this->set(compact('videos'));
}
view action :
public function admin_view($id = null) {
if (!$this->Player->exists($id)) {
throw new NotFoundException(__('Invalid player'));
}
$options = array('conditions' => array('Player.' . $this->Player->primaryKey => $id));
$this->set('player', $this->Player->find('first', $options));
}
You can save the referring page in the else section of the if/else structure of the edit function. Then use that stored value in the if (i.e., $this->request->is(array('post', 'put')) = TRUE section.
So your code would look something like:
public function admin_edit($id = null) {
if ($this->request->is(array('post', 'put'))) {
/* your other code */
$sendback = $this->Session->read('referer');
$this->Session->delete('referer');
$this->redirect($sendback);
} else {
/* your other code */
$this->Session->write('referer', $this->referer());
}
}

Cake php ajax layout cache

After any ajax request all subsequent non ajax request is returning with ajax layout instead of default layout.
Obs:
Ocurrs only on production enviroment.
Configure::write('Cache.disable', true); // Don't have any effect!
Cake Version 2.4.4
After 20 ~ 30 seconds the layout (default) is rendered.
config/core.php is identical.
I don't no why , and i loose 8 hours on that, any tip?
The controller (But any ajax on any controller cause the problem:
<?php
App::uses('AppController', 'Controller');
class NewslettersController extends AppController
{
public $paginate = array(
'limit' => 20,
'paramType' => 'querystring',
'order' => array(
'Newsletter.created' => 'DESC'
)
);
public function beforeFilter()
{
parent::beforeFilter();
$this->Auth->allow(array('add','push'));
}
public function admin_index()
{
$this->set('dataGrid', $this->paginate('Newsletter'));
}
public function admin_view($id = null)
{
$this->Newsletter->id = $id;
$result = $this->Newsletter->read();
if (!$result) {
$this->setFlashMessage('Cadastro não encontrado', 'error', array('action' => 'index'));
return false;
}
$this->set('dataGrid', $result);
}
public function admin_delete($id = null)
{
$this->Newsletter->id = $id;
if (!$this->Newsletter->exists()) {
$this->Session->setFlash('O item solicitado não foi encontrado!', 'alert_error');
$this->setFlashMessage('O item solicitado não foi encontrado!', 'error', array('action' => 'index'));
}
try {
if ($this->Newsletter->delete($id, false)) {
$this->setFlashMessage('Item excluído com sucesso!', 'success', array('action' => 'index'));
}
} catch (Exception $e) {
$this->setFlashMessage('Não foi possivel excluir este item pois existem itens atrelados a ele', 'error', array('action' => 'index'));
}
}
public function admin_search()
{
$this->autoRender = false;
$conditions = null;
if (isset($this->request->query['search']) && !empty($this->request->query['search'])) {
$conditions[] = array(
'OR' => array(
'Newsletter.name LIKE' => '%' . $this->request->query['search'] . '%',
'Newsletter.email LIKE' => '%' . $this->request->query['search'] . '%',
)
);
$this->paginate['conditions'] = $conditions;
$this->set('dataGrid', $this->paginate());
$this->render('admin_index');
}
}
//######################
//# FRONTEND #
//######################
public function push()
{
if($this->Newsletter->save($this->request->data))
{
$response = array("result"=>true,"id"=>$this->Newsletter->id);
}
else
{
$response = array("result"=>false,"errors"=>$this->Newsletter->validationErrors);
}
return new CakeResponse(array("body" => json_encode($response),"type" => "json"));
}
}
?>
You have to differentiate the layout for ajax in the method, please the following changes I made in the above code:
...
class NewslettersController extends AppController
{
public layout = 'default';
...
public function push() // if this is ajax function
{
$this->layout = 'ajax'; //put this for ajax functions only
....
}
...
}
Hope it helps!

cakePHP User Edit creates empty user

I currently have a probem with cakePHP:
If I open /users/edit/4 for example, a new empty user entry is created in the users database.
My UsersController:
public function edit($id = null) {
if (!$this->User->exists($id)) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
$this->request->data = $this->User->find('first', $options);
}
}
What am I doing wrong?
With kind regards,
Battlestr1k3
You did not add the $id
public function edit($id = null) {
if (!$this->User->exists($id)) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->request->is(array('post', 'put'))) {
$this->request->data['User']['id'] = $id;
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
$this->request->data = $this->User->find('first', $options);
}
}
TRY THIS
public function edit($id = null) {
if (!$this->User->exists($id)) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->request->is(array('post', 'put'))) {
$this->User->id = $id;
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
$this->request->data = $this->User->find('first', $options);
}
}
Thank you both for your fast answers, they both worked.
I was confused, because the code got even called when I made a GET-Request with a browser, but the problem was a javascript function which made a POST-Request in the background.
With kind regards,
Battlestr1k3

How to auto redirect user having Auto-Login cookie, using Auto-Login component in CakePHP 2x?

I am using CakePHP 2x with Auto-Login component. The problem is, I can write the stuff but, I am not sure how to implement it to read and authorize. When user arrives at the page, he still has the cookie in his browser but, how do I authorize it?
My Login script:
public function login() {
if ($this->Auth->user('id')) {
$this->redirect(array('action' => 'dashboard'));
}
if($this->request->data['User']['auto_login']):
$this->AutoLogin->write($this->request->data['User']['username'],
$this->request->data['User']['password']);
endif;
if ($this->request->is('post')) {
if ($this->Auth->login( )) {
//$this->redirect(array('controller' => 'users', 'action' => 'dashboard'));
return $this->redirect($this->Auth->redirect( ));
}
else
{
$this->Session->setFlash(__('Username or Password is incorrect'), 'default', array( ), 'auth');
}
}
This should be something like:
public function login()
{
if ($this->request->is('post'))
{
if ($this->Auth->login())
{
if ($this->request->data['User']['persist'] == '1')
{
$cookie = array();
$cookie['username'] = $this->data['User']['USER_LOGINNAME'];
$cookie['password'] = $this->data['User']['USER_PASSWORD'];
$this->Cookie->write('Auth.User', $cookie, true, '+4 weeks');
}
$this->redirect($this->Auth->redirect());
}
else
{
$this->Session->setFlash('Your username or password was incorrect.', 'default/flash-error');
}
}
else
{
$user = $this->Auth->user();
if (empty($user))
{
$cookie = $this->Cookie->read('Auth.User');
if (!is_null($cookie))
{
$user = $this->User->find('first', array('conditions' => array('USER_LOGINNAME' => $cookie['username'], 'USER_PASSWORD' => AuthComponent::password($cookie['password']))));
if ($this->Auth->login($user['User']))
{
$this->Session->delete('Message.auth');
$this->redirect($this->Auth->redirect());
}
else
{
$this->Cookie->delete('Auth.User');
}
}
}
else
{
$this->redirect($this->Auth->redirect());
}
}
}
This gives you the idea of how to achieve the same task, however, I used form fields according to my DB Structure.
Kindly change the form fields according to your DB Structure.

Categories