creating a dynamic sidebar with zend framework - php

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)

Related

How do I get my $_POST to recognize the values

$_POST won't recognize the value mailuid from the login form on this page or others (profile page).
$_Get methods do not work because of how the login system is built and unsecured.I need mailuid value to bring them to their own profiles page after login.
Login Form since its's post method I should be able to grab the value on other pages and this one
<div class="modal">
<div class = "modal-content">
<section class="section-default">
<h1>Login</h1>
<?php
if (!isset($_SESSION['Id'])) {
echo'<form action="includes/login.inc.php" method="post">
<input type="text" name="mailuid" placeholder="Username/E-mail...">
<input type="password" name="pwd" placeholder="Password...">
<button type="submit" name="login-submit">Login</button>
</form>';
} else if (isset($_SESSION['Id'])) {
echo '<div class="signup12">
You Do not have an account? Sign Up
</div>
<div class="forgotpwd">
Forgot your password?
</div>';
}
?>
</section>
</div>
</div>
Temporary check for the mailuid value. Supposed to grab the value form the login form a spit it back out, to check to see if it is recognized
<?php
$user = $_POST["mailuid"];
if (isset($_POST["mailuid"]))
{
$user = $_POST["mailuid"];
echo $user;
echo " is your username";
}
else
{
$user = null;
echo "no username supplied";
}
?>
First I would clean this up:
$user = $_POST["mailuid"];
if (isset($_POST["mailuid"]))
{
$user = $_POST["mailuid"];
echo $user;
echo " is your username";
}
else
{
$user = null;
echo "no username supplied";
}
Instead it can be written more concise:
$user = isset($_POST["mailuid"]) ? $_POST["mailuid"] : false;
if( $user ){
echo "{$user} is your username";
} else {
echo "no username supplied";
}
I prefer Boolean false over NULL, null just means it doesn't exist. Boolean false lets you know you checked it and it didn't exist. Generally should should access $_POST as few times as you can. This is because you should never trust $_POST.
$_Get methods do not work because of how the login system is built and unsecured.
Post is no more secure than get, it's quite easy to post anything to the page even without visiting the site by using something like PostMan etc. Once you assign it to a local variable you know you have at least normalized the data, even if you haven't sanitized it yet.
Also don't forget to call session_start before trying to access $_SESSION. Because of the vagueness of the question, it could be that the form works fine, just the session data isn't being maintained because you haven't started the session yet.. etc....
Hope it helps.
Personally I would clean up the HTML part that makes the form as well, so instead of this:
<div class="modal">
<div class = "modal-content">
<section class="section-default">
<h1>Login</h1>
<?php
if (!isset($_SESSION['Id'])) {
echo'<form action="includes/login.inc.php" method="post">
<input type="text" name="mailuid" placeholder="Username/E-mail...">
<input type="password" name="pwd" placeholder="Password...">
<button type="submit" name="login-submit">Login</button>
</form>';
} else if (isset($_SESSION['Id'])) {
echo '<div class="signup12">
You Do not have an account? Sign Up
</div>
<div class="forgotpwd">
Forgot your password?
</div>';
}
?>
</section>
</div>
</div>
I would do something like this:
<div class="modal">
<div class = "modal-content">
<section class="section-default">
<h1>Login</h1>
<?php if (!isset($_SESSION['Id'])){ ?>
<form action="includes/login.inc.php" method="post">
<input type="text" name="mailuid" placeholder="Username/E-mail...">
<input type="password" name="pwd" placeholder="Password...">
<button type="submit" name="login-submit">Login</button>
</form>
<?php }else{ ?>
<div class="signup12">
You Do not have an account? Sign Up
</div>
<div class="forgotpwd">
Forgot your password?
</div>';
<?php } ?>
</section>
</div>
</div>
See how much cleaner that is. Most of this is just readability issues. For example there is no need to check if isset($_SESSION['Id']) in the else if condition, because it's either set or not. This is one less place to maintain the session variable key, and it makes the code less convoluted.
As for the actual problem, as long as you are reaching the above code after submission of the form, it should work. So that leads me to believe that you have something wrong in the action.
You should get a clean page after going to includes/login.inc.php meaning there shouldn't be much in the way of HTML. One thing you can do that is real simple is just add at the top:
die(__LINE__.' of '.__FILE__);
$user = isset($_POST["mailuid"]) ? $_POST["mailuid"] : false;
//... other code
What this will do is die which kills PHP execution, but outputs the argument you passed in. In this case I'm just putting the line and file that the die is on, that way it's easier to find later. But the point is to see if you are even hitting the correct ending script or the forms action/endpoint.
I only suggest this because you are really vague in what it's current behaviour is
$_POST won't recognize the value mailuid from the login form on this page or others (profile page).
For example, this doesn't tell me if you are even hitting the right page. Now had you said something like "all it does is output no username supplied". Then I would at lest know that. As I said above it could be just an omission of sesion_start() which must be called before attempting to access any $_SESSION stuff. You should call it only once, at the top of each page that uses sessions.
Although it's not a solution, it was too much to post in a comment. I would really like to help you more, but there just isn't enough information to go on.

How to Include/Use Composer Bootstrap in CodeIgniter?

I want to include/use bootstrap in all my controllers/views without having to load each css/js in each view file, so I have done this:
Installed Composer Bootstrap
composer require twbs/bootstrap
My Index Controller:
public function index() {
// Composer Autoloader
require VENDORPATH.'autoload.php';
require_once BASEPATH.'core/CodeIgniter.php';
echo '<div class="section jumbotron text-center">Yu in index son.</div>';
}
VENDORPATH = myhomefolder../vendor/
vendor/autoload.php
// autoload.php #generated by Composer
require_once __DIR__ . '/composer' . '/autoload_real.php';
return ComposerAutoloaderInit9d54f40b1177ed0ebd8d1d378ec06d06::getLoader();
/composer.json
{
"require": {
"twbs/bootstrap": "^3.3"
}
}
I don't know what to do now, I have searched all the web but it only says things about other packages or something not related and Im stuck in this right now, if someone in advance can help, I would appreciate, thank you.
First
create header and footer which consists of bootstrap css and js
and put them into a templates folder in your views folder
In controller use them like this to every method:
public function index() {
$data['title'] = 'home';
$this->load->view('templates/bootstrap_header',$data);
$this->load->view('index',$data);
$this->load->view('templates/bootstrap_footer');
}
What I do, is that I use a "main" template, which includes the css, js, head, body, etc... Then, inside my body tag, I do the $this->load->view('folder/function', $data); which processes the internal view... Do I make sense ???
Main View:
<html>
<head>
<?php $this->load->view('html/head', $view_data); ?>
</head>
<body lang="es">
<div class="container">
<?php $this->load->view('html/header')?>
<div class="contenido">
<?php if (isset($which_view)) $this->load->view($which_view, $view_data)?>
</div>
<?php $this->load->view('html/footer');?>
</div>
</body>
</html>
And finally, I create a small "inner-view" in a folder named after my controller, named after my function , so everything stays in sync...
<div class="formatoLogin modulo">
<?php echo form_open('login/doLogin', array('id'=>'formLogin', 'class'=>'form-signin'))?>
<?php $this->load->view('mod/notificacion')?>
<h1>Acceso Restringido</h1>
<div class="form-group">
<label for="Usuario" class="control-label">Usuario:</label>
<input type="text" name="Usuario" id="Usuario" class="form-control" />
</div>
<div class="form-group">
<label for="Contrasenia" class="control-label">ContraseƱa:</label>
<input type="password" name="Contrasenia" id="Contrasenia" class="form-control">
</div>
<div class="form-group text-center">
<button class="btn btn-info">Acceder</button>
</div>
<?php if($this->uri->segment(3)=='redirect'){?>
<input type="hidden" name="redirect" value="<?php echo $this->uri->segment(4).'/' . $this->uri->segment(5).'/' . $this->uri->segment(6) ?>">
<?php } ?>
<?php echo form_close()?>
</div>
Do I make sense? I hope it works cause it's saved me tons of includes...

Php code not working as expected

I have created a simple one user one password custom login to protect a specific page. In the page I want to protect (gallery.php) I have the following code:
<?php
session_start();
if (!$_SESSION["Login"]){
header("Location:index.php");
}
?>
<p>some protected content</p>
As so if user enters url http://www.example.com/pics/gallery.php
the pages take you to http://www.example.com/pics/index.php
In index I have the fallowing code:
<?php
session_start();
if(($_POST['username'] == "user1") && ($_POST['pass'] == "pass1"))
{
$_SESSION["Login"]= true;
header("Location:gallery.php");
}
else
{ ?>
<?php include 'header.php'; ?>
<div class="wlcm">
<div class="login-box wow fadeInUp">
<h2 class="lgn-tle">Please login</h2>
<form name="login" method="post" accept-charset='UTF-8'>
<input
type="text"
id="username"
name="username"
class="wow fadeInUp"
data-wow-delay=".2s"
placeholder="User"
required="true">
<input
type="password"
id="pass"
name="pass"
class="wow fadeInUp"
data-wow-delay=".4s"
placeholder="Password"
required="true">
<input
type="submit"
name="Submit"
class="submit wow fadeInUp"
data-wow-delay=".6s"
value="Login">
</form>
</div>
</div>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo "incorrect";
}
}
?>
While the code on gallery.php works and takes user to login.php if he's not already logged in. When I submit correct info in index.php I just get a blank page without any php errors.
It seems like header("Location:gallery.php"); doesn't do anything and I have tried absolute paths and spaces after "Location".
Am I missing something here?
All I want is a custom login using an html form instead of .htaccess and to do so in the simples and cleaner possible way.
Why you include login.php like <include'login.php'/>?
it should be <?php include 'login.php'; ?>
And form should have action set to login.php or index.php if you're checking if values are set on index.php

Form is not getting displayed in codeigniter.

I am new to code igniter. I've created a form but it is not display properly.
When I put
<?php echo form_open('sms'); ?> instead of <form action="">tag
here in my form and controller, I can't understand why it is not displayed.
<?php echo form_open('sms'); ?>
<p>
<label><strong>Username</strong>
<input type="text" name="textfield" class="inputText" id="textfield" />
</label>
</p>
<p>
<label><strong>Password</strong>
<input type="password" name="textfield2" class="inputText" id="textfield2" />
</label>
</p>
<input type="submit" value="Authentification" name="auth" />
<label>
<input type="checkbox" name="checkbox" id="checkbox" />
Remember me</label>
</form>
and my controller is
<?php
class sms extends CI_Controller{
function school(){
$this->load->view('school/index.php');
if($this->input->post('auth',TRUE)){
$this->load->view('school/dashboard.php');
}
else{
$this->load->view('school/index.php');
}
}
}
?>
If this is your entire script for the most part, it looks like you need to load the helper first from the CodeIgniter Form Helper Page.
If you don't have this line, try adding it before the form_open() function:
<?php $this->load->helper('form'); ?>
While I have used CodeIgniter, it's been a while. Let me know if that changes the result.
Edit: Since you've chosen my answer I'll include this one, credits go out to devo:
You could change </form> to: <?php echo form_close(); ?>. There are pros and cons for this method though, and without using arguments you might be better off sticking with </form>.
I'll explain further:
<div class="registration">
<div class="form-box">
<?php $this->load->helper( 'form' ); ?>
<?php $end = '</div></div>'; ?>
<?php echo form_open( 'register' ); ?>
<!-- Form Inputs Here -->
<?php echo form_close( $end ); ?>
<!-- Echos '</form></div></div>' -->
So for closing the form without arguments, the </form> tag works best, both by performance and simplicity. The example used above is a rather simplistic view of what you can do with it, since what I wrote is not very efficient either.
However, this is still php we're talking about, so perhaps the craftier among us could put it to better use.
End Edit
Have you loaded the form helper? You can use $this->load->helper('form'); in your controller action, her inside function school(). You can then use form helper in the view pages.
Load form helper,
$this->load->helper('form');
And use,
echo form_close()
Instead of,
</form>
First in your Controller put in:
$this->load->helper('form');
And Change </form> to :
<?php echo form_close(); ?>

PHP: When echo a JavaScript alert, text input size decreases

I have a basic HTML form like this. When submitted, it checks for empty fields and alert the user that both fields are required. After alert, user is redirected to the same page but text input sizes decrease. How can I retain the same width, height after PHP "echo"?
Screenhot 1:
Screenshot 2:
Screenshot 3:
Controller:
function validate_user_pass() {
$this->load->library('form_validation');
$this->form_validation->set_rules('username','Email', 'required|max_length[30]');
$this->form_validation->set_rules('password','Password', 'required|max_length[32]');
if ($this->form_validation->run() == FALSE ) {
//$this->load->view('login_error');
echo '<script type="text/javascript">alert("Required fields are empty!");</script>';
$this->index();
}
else {
$this->verify();
}
}
View:
<?php echo form_open('login/validate_user_pass'); ?>
<div class="login">
<div class="login-screen">
<div class="login-icon">
<img src="<?php echo base_url(); ?>assets/images/1374606542_newsstand_ios7_ios_7.png" alt="logo">
<h4>Hotel<small>Management System</small></h4>
</div>
<div class="login-form">
<img src="<?php echo base_url(); ?>assets/ico/logo_small.png" alt="logo">
<div class="control-group">
<input type="text" class="login-field" name="username" value="<?php echo set_value('username'); ?>" placeholder="Enter username" id="login-name">
</div>
<div class="control-group">
<input type="password" class="login-field" name="password" value="" placeholder="Password" id="login-pass">
</div>
<input type="submit" class="btn btn-primary" value="Login">
</div>
</div>
</div>
<?php echo form_close(); ?>
it may be late to answer but same problem was with me. I overcome this by putting the error popup box out of the header. I searched everywhere and ended up here.
My code was something like this before:
<?php
//some code for verification and echo out/show error message
?>
<html>
<!--HTML code that has form-->
</html>
What I did was something like this:
<html>
<head>
<title></title>
</head>
<body>
<?php
//some code for verification and echo out/show error message
?>
<div>
<!--HTML code that has form-->
</div>
</body
</html>
This problem mostly exist because of echo present in header part. You can check your views for this and that may solve the problem.
This typically happens when a php error or var_dump() is on the page, but you may not see it because it is hidden under a fixed header div, for example.
Look at your View source & check if there aren't php errors on the page

Categories