This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

Authentication bridge between Django and PHP

Project Description
django-php-bridge - Authentication betwen Django and PHP


`Django-PHP-Bridge`_ is a Django authentication backend that allows your Django
project to seemlessly pass users to and from a PHP application. This allows
you to build an application with both PHP and Django components while keeping a
solid user experience.

Whether you're porting from PHP to Django, from Django to PHP, integrating two
distinct applications or building a hybrid app, `Django-PHP-Bridge`_ aims to
help make the distinction irrelevant to your users.

General Approach

There are several different ways to approach this problem, mostly revolving
around which side's default behavior you use and which side needs more
customization. In general, we've taken (and this documentation assumes) that
Django's default behavior should be used where possible. However, it is
completely possible to use the provided session backend as part of a more
PHP-centric approach.

Django Defaults Used

* ``django.contrib.auth.models.User`` is used to store Users, with the standard
django Profile extension recommended for additional fields.
* The database is used as the session store, but this could easily be

PHP Defaults Used

* ``django-php-bridge.backends.db`` uses PHP's native serialization format to
store session data.


This usage guide assumes a few things about your setup.

1. You're using Django's ``django.contrib.auth.backends.ModelBackend`` as your
authentication backend and you want to use ``django.contrib.auth.models.User``
for storing basic user information.
2. Your PHP and Django projects share a database. That's how the session
coordination is accomplished.
3. If you had a custom schema to store your user and profile information,
you've already converted it to Django's schema.

Usage: Django Side

On the django side of your project, installation is fairly simple.

Install `Django-PHP-Bridge`_::

$ pip install django-php-bridge

Configure your Django project to use the PHP-compatible session backend by
adding the following to your ```` ::

SESSION_ENGINE = 'django_php_bridge.backends.db'

Let your Django project know that you'll be using the PHP side of your project
to do actual logins. You do this by setting the ``LOGIN_URL`` setting in
```` to point to the PHP-served URL that will be handling your
login. eg.::


Usage: PHP Side

Installation and setup on the PHP side is complicated by the fact that PHP
applications are all generally very different. A helper/guide for using
Django-PHP-Bridge with common PHP frameworks like `CakePHP`_ and `Symfony`_
would be easier to write (and would be an appreciated contribution).

In general, the steps involved are:

Create and Use a Compatible Session Table

The session table you use needs to be compatible with the schema that Django
expects. The exact SQL to create the table will vary, but the Django Docs on
the `sql command`_ show us an easy way to obtain the SQL from your django
project by running::

$ sql sessions

If you're using MySQL, you can use ``contrib/mysql/django_session_table.sql``

Alternatively, you can use Django's syncdb to create the table::

$ syncdb

.. _`sql command`:

Place the Appropriate Session-Handler on Every Page

PHP allows for `custom session handlers`_ to be defined, which allows us to
use the django_session table we created above. The session handler you use will
need to be aware of the django_session table's schema and you'll need to
register this session handler on every page *before* calling ``session_start();``.

An example session handler class is provided in

.. _`custom session handlers`:

Create and Use a Compatible User Table

In order for any reasonable level of integration, most projects will need to
know who users are on both the PHP and Django side. Because most general
PHP projects vary greatly in how they store their user information, if coming
from an existing PHP project, this will probably require some custom work to
convert user data. Django applications generally use a User model plus a
Profile model to store user data. See the `Django Auth Documentation`_ for

Included is an example of a PHP class that relies on the same schema as
``django.contrib.auth.models.User`` as an example and starting point. It knows
a little bit about how Django stores passwords and what fields are necessary,
but it will certainly need tweaking to work with your existing PHP
project. The file is located at ``contrib/php/user.class.php``.

Suggestions and contributions to make this part of the integration process
easier are welcome.

.. _`Django Auth Documentation`:

Configure URLs Handled by PHP vs Django

The final piece of integration will be to tell your web server how to determine
if a given request should be resolved by the Django side or by the PHP side.
This means changing your configuration so that for example, everything at
``/account`` is served by Django and everything at ``/blog`` is served by PHP.
If you're using different domains or subdomains to separate the side of your app,
then you can ignore this step.

Generally, to keep this part sane, you'll want to file good URL practices and
separate which side of your project handles particular tasks and domain objects.
Django's application-centric ```` configuration makes this easy.
Particular attention should be paid with regards to which side of your project
should handle logging in and logging out. It's generally simpler if either
only Django or only PHP handles both logging in and logging out users and
probably simpler if that same side handles registration and account editing.

In the case of `Apache2`_ running `mod_wsgi`_ for Django and mod_php (or
similar) for PHP, the separation can be accomplished inside a VirtualHost file.
An example vhost file is provided at ``contrib/apache2/vhost_conf``.


This authentication backend was extracted from code used in production by
a saas policy management start called `PolicyStat`_ during their multi-year
conversion from a PHP application to a `Django`_ application. You can read
a bit about their `PHP to Django Conversion`_.

`PolicyStat`_ has sense converted to 100% Django and is no longer using this
approach in production, but the hope is that someone who is will be interested
in taking an active role in this project.


All development on Django-PHP-Bridge happens at Github:

You are highly encourage to contribute to the improvement of Django-PHP-Bridge.
We would especially love contributions along the lines of how to integrate with
specific PHP frameworks.

Bug tracker

If you have any suggestions, bug reports or questions please report them
to our issue tracker at

Also feel free to tweet @weswinham on twitter.

.. For full documenation, you can build the `sphinx`_ documentation yourself or
.. vist the `online Django-PHP-Bridge documentation`_

.. _`Django-PHP-Bridge`:
.. _`Policystat`:
.. _`Django`:
.. _`CakePHP`:
.. _`Symfony`:
.. _`Apache2`:
.. _`mod_wsgi`:
.. _`PHP to Django Conversion`:
.. _`sphinx`:
.. _`online Django-PHP-Bridge documentation`:
Release History

Release History

This version
History Node


History Node


History Node


Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
django-php-bridge-0.1.1.tar.gz (9.0 kB) Copy SHA256 Checksum SHA256 Source Feb 19, 2014

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting