Question

i'm new to zend and i almost understand how it works with the mvc, but i'm trying to use only zend_auth in a non mvc project. The login(auth) part works ( even if i'm not sure if it's the best way to do it), but i can't figure out how to get the users data(which is stored when the user logs in) in another page. here is my login.php:

<?php
ini_set('display_errors', 1);
// define an absolute path to library directory
// you don't have to set a constant but it is just good practice 
// you can also use a variable or add the path directly below
define('APPLICATION_LIBRARY','c:/xampp/htdocs/website/');
// Note again: the path is the parent of your Zend folder, not the Zend folder itself.
// now set the include path
set_include_path(implode(PATH_SEPARATOR, array(
  APPLICATION_LIBRARY, get_include_path(),
)));
include("config.php");





        // i'm testing with a username and password that i know is inside database in the actual code is: $username= $_POST['username'] and the same for password.
         $username ='userx';
         $password = 'passx';
        $auth = Zend_Auth::getInstance();

         $adapter = new Zend_Auth_Adapter_DbTable($db);
         $adapter ->setTableName('profiles')
                  ->setIdentityColumn('username')
                  ->setCredentialColumn('password')
                  ->setIdentity($username)
                  ->setCredential($password);


         $result = $auth->authenticate($adapter);





            switch($result->getCode()) {
        case Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND:
            $errorMessage = "Error: Name not correct.";

            // i'm sending username and password to another login that is exactly like this one, but searches in another table
            header("Location: login2.php?pass=".$password."&user=".$username);
            break;

        case Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID:
            $errorMessage = "Error: pass and user are not valid";
            echo $errorMessage;

         header("Location: login2.php?pass=".$password."&user=".$username);
            break;

        case Zend_Auth_Result::SUCCESS:



            $adapter->getResultRowObject();
            $authStorage = $auth->getStorage(); // set storage. Session default

            $authStorage->write($result); // save user data


            if(isset($_POST['remember'])) {
                Zend_Session::rememberMe(60*60*24*7*4);
                $remember=$_POST['remember'];
                    }
            // send user to page they originally request, with username and pass because i don't know how to get them there from what i've stored before...i know it's not good.
       header("Location: profile_page.php?pass=".$password."&user=".$username."&remember=".$remember);
            break;

        default:
            $errorMessage = "Error.";
            break;
    }
?>

this is my config.php file:

<?php
error_reporting(E_ALL);

require_once('Zend/Session/Namespace.php');
require_once('Zend/Session.php');
require_once('Zend/Db/Table/Abstract.php');
require_once('Zend/Auth/Adapter/DbTable.php');
require_once('Zend/Auth.php');
require_once('Zend/Db/Expr.php');
require_once('Zend/Auth/Exception.php');
require_once('Zend/Controller/Action.php');


$params = array(
    'host'     => '127.0.0.1',
    'username' => 'root',
    'password' => '',
    'dbname'   => 'website',
);

try {
    $db = Zend_Db::factory('Pdo_Mysql', $params);
    $db->getConnection();
} catch(Zend_Db_Adapter_Exception $e) {
    die("<pre>There was an error connecting to the server</pre>"); //Probably bad login or mysql daemon is not running.
} catch(Zend_Exception $e) {
    die("<pre>Something failed to load</pre>"); //factory() probably failed to load the adapter class
}

$users = new Zend_Session_Namespace('Zend_Auth');




?>

I want to get the data that is stored when the user is authenticated in his profile page.

Was it helpful?

Solution

You are wrting wrong thing in storage instead do

        $authStorage->write($adapter->getResultRowObject();); // save user data
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top