Discussion:
How to intergrate Django with PHP
(too old to reply)
rajivb
2008-02-27 14:48:10 UTC
Permalink
Hey guys,

I m new to web development..By the way i created one application in
PHP. now i want that for login part/admin part i use Django.. So how
can i intergrate these two(i mean existing php code with Django)??

Please help .....
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users-***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
Charles
2008-02-27 15:06:15 UTC
Permalink
Hi Rajivb,

That's a really odd idea. Why on earth do you want to do such a thing?

You can use Django to build your whole application (admin interface +
front-end interface).
There is no point to mix php code and python code in that case.
Either you choose a php web framework (such as Symfony) or a python
web framework (such as Django).

If you have already started to build something using php then I would
advise you to go for a php framework. Symfony (http://www.symfony-
project.org/) is a MVC web framework that works the same way as Django
and provides, as Django, an auto-generated admin (what you call 'login
part/admin part'). You should give a try...

Personally, I prefer Django by far.

Charles.
Post by rajivb
Hey guys,
I m new to web development..By the way i created one application in
PHP. now i want that for login part/admin part i use Django.. So how
can i intergrate these two(i mean existing php code with Django)??
Please help .....
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users-***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
rajivb
2008-02-27 15:23:38 UTC
Permalink
Hey Charles,

Really thanx for ur time and great suggestion..

Actually i got stuck in a problem..I am having a complete application
written in PHP. You can assume that i need to integrate it with a lot
of applications which supports Django interface. Due to time
contraints it not possible for me to rewrite the whole code...So i
thought of an solution like if for log-in purpose somehow i'll use
django so that i'll be able to integrate it with other apps also..so
thats why i asked is there any way so that i can have login thing in
Django and other things in PHP..

If there is a way so that i cn integrate Django with PHP with less
number of cons so may be i cn thnk of it ;-)
Thanks again for your time ;)

Rajivb
Post by Charles
Hi Rajivb,
That's a really odd idea. Why on earth do you want to do such a thing?
You can use Django to build your whole application (admin interface +
front-end interface).
There is no point to mix php code and python code in that case.
Either you choose a php web framework (such as Symfony) or a python
web framework (such as Django).
If you have already started to build something using php then I would
advise you to go for a php framework. Symfony (http://www.symfony-
project.org/) is a MVC web framework that works the same way as Django
and provides, as Django, an auto-generated admin (what you call 'login
part/admin part'). You should give a try...
Personally, I prefer Django by far.
Charles.
Post by rajivb
Hey guys,
I m new to web development..By the way i created one application in
PHP. now i want that for login part/admin part i use Django.. So how
can i intergrate these two(i mean existing php code with Django)??
Please help .....- Hide quoted text -
- Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users-***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
kevinski
2008-02-27 15:35:35 UTC
Permalink
Post by rajivb
thats why i asked is there any way so that i can have login thing in
Django and other things in PHP..
If there is a way so that i cn integrate Django with PHP with less
number of cons so may be i cn thnk of it ;-)
Did you ever consider embedding PHP pages into your Django app using
iframes? I borrowed a couple PHP apps that I would have had a lot of
trouble writing in Python due to complex API usage, so I embedded them
and they work great and nobody can tell they are not part of the app.

Just a suggestion.

Kevin
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users-***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
rajiv bammi
2008-02-27 15:38:18 UTC
Permalink
Hey kevinski,

Thanks for ur suggestion..

Can u please explain a bit...this can be a gud solution for me...Can u
please eplain a bit ;)
Post by kevinski
Post by rajivb
thats why i asked is there any way so that i can have login thing in
Django and other things in PHP..
If there is a way so that i cn integrate Django with PHP with less
number of cons so may be i cn thnk of it ;-)
Did you ever consider embedding PHP pages into your Django app using
iframes? I borrowed a couple PHP apps that I would have had a lot of
trouble writing in Python due to complex API usage, so I embedded them
and they work great and nobody can tell they are not part of the app.
Just a suggestion.
Kevin
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users-***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
kevinski
2008-02-27 15:59:40 UTC
Permalink
The iframe, or inline frame, is an html tag that seamlessly embeds one
page inside another. Very simple. Learn how to use here:
http://www.w3schools.com/tags/tag_iframe.asp

You will be able to embed a PHP page inside your Django template with
the following code.

<iframe src="/your_php_page.php" width="100%">
</iframe>

You can host your PHP pages the same way you host media (if you are
using Apache).

<Location "/php_pages">
SetHandler None
</Location>

I hope that helps :)

Kevin
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users-***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
rajiv bammi
2008-02-27 16:01:50 UTC
Permalink
yup ;-)
thnx a lot dear
Post by kevinski
The iframe, or inline frame, is an html tag that seamlessly embeds one
http://www.w3schools.com/tags/tag_iframe.asp
You will be able to embed a PHP page inside your Django template with
the following code.
<iframe src="/your_php_page.php" width="100%">
</iframe>
You can host your PHP pages the same way you host media (if you are
using Apache).
<Location "/php_pages">
SetHandler None
</Location>
I hope that helps :)
Kevin
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users-***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
gordyt
2008-02-27 15:43:06 UTC
Permalink
Hi rajivb,

I was in a spot where I had to tie an existing PHP site in with a
Django site. I did not want folks to be able to access the PHP part
of the site unless the following conditions applied:
(1) they were logged in with the Django app, and
(2) they did NOT belong to the "Sales" group

Here is the PHP code that I used to implement that:

---------- cut here ----------
<?php
require(dirname($_SERVER['DOCUMENT_ROOT']) . "/config/db.php");
$authenticated = false;
if(array_key_exists('sessionid',$_COOKIE)) {
$sessionid = $_COOKIE['sessionid'];
$dbj = get_django();
$result = $dbj->query("select * from django_session where
session_key='$sessionid'");
if($row = $result->fetch(PDO::FETCH_ASSOC)) {
$raw_data = base64_decode($row['session_data']);
$raw_arr = explode('_auth_user_id', $raw_data);
$len = ord(substr($raw_arr[1],3,1));
$dec = 0;
while($len > 0) {
$dec <<= 8;
$byte = ord(substr($raw_arr[1],3+$len,1));
$dec |= $byte;
$len -= 1;
}

$result = $dbj->query("select is_active from auth_user where
id=$dec");
if($row = $result->fetch(PDO::FETCH_ASSOC)) {
if($row['is_active']) {
$result = $dbj->query("select count(*) from
auth_user_groups t1 left join auth_group t2 on t1.group_id=t2.id" .
" where t1.user_id=$dec and t2.name='Sales'");
$row = $result->fetch(PDO::FETCH_NUM);
if($row[0] == 0) {
$authenticated = true;
}

}
}
}
else {
$error_info = $result->errorInfo();
syslog(LOG_DEBUG, sprintf("hbadmin/index.php: SQLSTATE=%s,
error_code=%s, error_message=%s",
$error_info[0], $error_info[2], $error_info[2]));
}
}

if(!$authenticated) {
header("Location: /accounts/login?next=/hbadmin/");
exit;
}

?>
---------- cut here ----------

A few notes...

The get_django() function (defined elsewhere) returned a PHP PDO
object to my Django database. Basically the code makes sure they have
a Django session. It grabs their session data, which is a base64
encoded pickled object. It's not easy to decode a pickled python
object in PHP, but I decode just enough to grab the user id of the
logged in user.

By the way, this is on a secure site and both PHP and Django are
configured to use secure cookies. Also I really need to update that
first query so that an expired session key cannot be returned.. Right
now that app is in-house only and I will update it before it is
online.

--gordy
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users-***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
rajiv bammi
2008-02-27 15:50:37 UTC
Permalink
Hi Gordy,

Thanks a lot for quick and very helpful response..It seems that u solved my
problem ;)
But becuase i m just newer to Django and python i just need to thnk for a
while and have to see whether things in reality works for me and not ;-)

By the way i thnk ur inputs really are very useful for me at this point of
time ;-)
Thanks again dear ;)


Byeee
rajivb
Post by gordyt
Hi rajivb,
I was in a spot where I had to tie an existing PHP site in with a
Django site. I did not want folks to be able to access the PHP part
(1) they were logged in with the Django app, and
(2) they did NOT belong to the "Sales" group
---------- cut here ----------
<?php
require(dirname($_SERVER['DOCUMENT_ROOT']) . "/config/db.php");
$authenticated = false;
if(array_key_exists('sessionid',$_COOKIE)) {
$sessionid = $_COOKIE['sessionid'];
$dbj = get_django();
$result = $dbj->query("select * from django_session where
session_key='$sessionid'");
if($row = $result->fetch(PDO::FETCH_ASSOC)) {
$raw_data = base64_decode($row['session_data']);
$raw_arr = explode('_auth_user_id', $raw_data);
$len = ord(substr($raw_arr[1],3,1));
$dec = 0;
while($len > 0) {
$dec <<= 8;
$byte = ord(substr($raw_arr[1],3+$len,1));
$dec |= $byte;
$len -= 1;
}
$result = $dbj->query("select is_active from auth_user where
id=$dec");
if($row = $result->fetch(PDO::FETCH_ASSOC)) {
if($row['is_active']) {
$result = $dbj->query("select count(*) from
auth_user_groups t1 left join auth_group t2 on t1.group_id=t2.id" .
" where t1.user_id=$dec and t2.name='Sales'");
$row = $result->fetch(PDO::FETCH_NUM);
if($row[0] == 0) {
$authenticated = true;
}
}
}
}
else {
$error_info = $result->errorInfo();
syslog(LOG_DEBUG, sprintf("hbadmin/index.php: SQLSTATE=%s,
error_code=%s, error_message=%s",
$error_info[0], $error_info[2], $error_info[2]));
}
}
if(!$authenticated) {
header("Location: /accounts/login?next=/hbadmin/");
exit;
}
?>
---------- cut here ----------
A few notes...
The get_django() function (defined elsewhere) returned a PHP PDO
object to my Django database. Basically the code makes sure they have
a Django session. It grabs their session data, which is a base64
encoded pickled object. It's not easy to decode a pickled python
object in PHP, but I decode just enough to grab the user id of the
logged in user.
By the way, this is on a secure site and both PHP and Django are
configured to use secure cookies. Also I really need to update that
first query so that an expired session key cannot be returned.. Right
now that app is in-house only and I will update it before it is
online.
--gordy
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users-***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
rajiv bammi
2008-02-27 16:50:16 UTC
Permalink
Hey i thnk a lot on this code....i need to do almost the same thing..but i
didnt get how u intergate PHP with django.
Is it by this line??
$dbj = get_django();

if yes , then so my confusion is get_django(); is a funtion written in
php..how it will integrate with django ..

i need to do almost same thing wat u did..like i need to show some pages to
the user only if they r authrized by django app..
So i got stuck at that how i cn integrate this appl with my PHP code..

Can u please explain a bit ...

Thanks a lot for ur concerns and suggestions.. ;)
Post by gordyt
Hi rajivb,
I was in a spot where I had to tie an existing PHP site in with a
Django site. I did not want folks to be able to access the PHP part
(1) they were logged in with the Django app, and
(2) they did NOT belong to the "Sales" group
---------- cut here ----------
<?php
require(dirname($_SERVER['DOCUMENT_ROOT']) . "/config/db.php");
$authenticated = false;
if(array_key_exists('sessionid',$_COOKIE)) {
$sessionid = $_COOKIE['sessionid'];
$dbj = get_django();
$result = $dbj->query("select * from django_session where
session_key='$sessionid'");
if($row = $result->fetch(PDO::FETCH_ASSOC)) {
$raw_data = base64_decode($row['session_data']);
$raw_arr = explode('_auth_user_id', $raw_data);
$len = ord(substr($raw_arr[1],3,1));
$dec = 0;
while($len > 0) {
$dec <<= 8;
$byte = ord(substr($raw_arr[1],3+$len,1));
$dec |= $byte;
$len -= 1;
}
$result = $dbj->query("select is_active from auth_user where
id=$dec");
if($row = $result->fetch(PDO::FETCH_ASSOC)) {
if($row['is_active']) {
$result = $dbj->query("select count(*) from
auth_user_groups t1 left join auth_group t2 on t1.group_id=t2.id" .
" where t1.user_id=$dec and t2.name='Sales'");
$row = $result->fetch(PDO::FETCH_NUM);
if($row[0] == 0) {
$authenticated = true;
}
}
}
}
else {
$error_info = $result->errorInfo();
syslog(LOG_DEBUG, sprintf("hbadmin/index.php: SQLSTATE=%s,
error_code=%s, error_message=%s",
$error_info[0], $error_info[2], $error_info[2]));
}
}
if(!$authenticated) {
header("Location: /accounts/login?next=/hbadmin/");
exit;
}
?>
---------- cut here ----------
A few notes...
The get_django() function (defined elsewhere) returned a PHP PDO
object to my Django database. Basically the code makes sure they have
a Django session. It grabs their session data, which is a base64
encoded pickled object. It's not easy to decode a pickled python
object in PHP, but I decode just enough to grab the user id of the
logged in user.
By the way, this is on a secure site and both PHP and Django are
configured to use secure cookies. Also I really need to update that
first query so that an expired session key cannot be returned.. Right
now that app is in-house only and I will update it before it is
online.
--gordy
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users-***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
rajiv bammi
2008-02-27 17:02:45 UTC
Permalink
Hi Gordy,

Hey i thnk a lot on this code....i need to do almost the same thing..but i
didnt get how u intergate PHP with django.
Is it by this line??
$dbj = get_django();

if yes , then so my confusion is get_django(); is a funtion written in
php..how it will integrate with django ..

i need to do almost same thing wat u did..like i need to show some pages to
the user only if they r authrized by django app..
So i got stuck at that how i cn integrate this appl with my PHP code..

Can u please explain a bit ...

Thanks a lot for ur concerns and suggestions.. ;)
Post by rajiv bammi
Hey i thnk a lot on this code....i need to do almost the same thing..but i
didnt get how u intergate PHP with django.
Is it by this line??
$dbj = get_django();
if yes , then so my confusion is get_django(); is a funtion written in
php..how it will integrate with django ..
i need to do almost same thing wat u did..like i need to show some pages
to the user only if they r authrized by django app..
So i got stuck at that how i cn integrate this appl with my PHP code..
Can u please explain a bit ...
Thanks a lot for ur concerns and suggestions.. ;)
Post by gordyt
Hi rajivb,
I was in a spot where I had to tie an existing PHP site in with a
Django site. I did not want folks to be able to access the PHP part
(1) they were logged in with the Django app, and
(2) they did NOT belong to the "Sales" group
---------- cut here ----------
<?php
require(dirname($_SERVER['DOCUMENT_ROOT']) . "/config/db.php");
$authenticated = false;
if(array_key_exists('sessionid',$_COOKIE)) {
$sessionid = $_COOKIE['sessionid'];
$dbj = get_django();
$result = $dbj->query("select * from django_session where
session_key='$sessionid'");
if($row = $result->fetch(PDO::FETCH_ASSOC)) {
$raw_data = base64_decode($row['session_data']);
$raw_arr = explode('_auth_user_id', $raw_data);
$len = ord(substr($raw_arr[1],3,1));
$dec = 0;
while($len > 0) {
$dec <<= 8;
$byte = ord(substr($raw_arr[1],3+$len,1));
$dec |= $byte;
$len -= 1;
}
$result = $dbj->query("select is_active from auth_user where
id=$dec");
if($row = $result->fetch(PDO::FETCH_ASSOC)) {
if($row['is_active']) {
$result = $dbj->query("select count(*) from
auth_user_groups t1 left join auth_group t2 on t1.group_id=t2.id" .
" where t1.user_id=$dec and t2.name='Sales'");
$row = $result->fetch(PDO::FETCH_NUM);
if($row[0] == 0) {
$authenticated = true;
}
}
}
}
else {
$error_info = $result->errorInfo();
syslog(LOG_DEBUG, sprintf("hbadmin/index.php: SQLSTATE=%s,
error_code=%s, error_message=%s",
$error_info[0], $error_info[2], $error_info[2]));
}
}
if(!$authenticated) {
header("Location: /accounts/login?next=/hbadmin/");
exit;
}
?>
---------- cut here ----------
A few notes...
The get_django() function (defined elsewhere) returned a PHP PDO
object to my Django database. Basically the code makes sure they have
a Django session. It grabs their session data, which is a base64
encoded pickled object. It's not easy to decode a pickled python
object in PHP, but I decode just enough to grab the user id of the
logged in user.
By the way, this is on a secure site and both PHP and Django are
configured to use secure cookies. Also I really need to update that
first query so that an expired session key cannot be returned.. Right
now that app is in-house only and I will update it before it is
online.
--gordy
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users-***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
gordyt
2008-02-27 21:23:31 UTC
Permalink
In my case I am running a very current version of PHP and I have
packges installed that let me access my Django database. For this
particular app the DJango database is using MySQL and the get_djang()
line is a PHP function, defined elsewhere, that returns a PDO object.

You of course do not have to use PDO at all, you can use the MySQL-
specific database functions if you like. But, assuming that you DO
want to use PDO:

/**
* Connect to django database.
* Returns:
* A PDO object
* Throws:
* PDOException - if connection error occurs
*/
function get_django() {
global $DJANGO_HOST, $DJANGO_NAME, $DJANGO_USER, $DJANGO_PW;
$dsn = sprintf("mysql:host=%s;dbname=%s", $DJANGO_HOST,
$DJANGO_NAME);
return new PDO($dsn, $DJANGO_USER, $DJANGO_PW);
}

--g
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users-***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
rajiv bammi
2008-02-28 04:40:54 UTC
Permalink
Hey Gordyt,

Thanks a lot dear ..You made my day ;-) i thnk i got the solution ;-)

Thanks again..

Tk cr..
Byyee
Post by gordyt
In my case I am running a very current version of PHP and I have
packges installed that let me access my Django database. For this
particular app the DJango database is using MySQL and the get_djang()
line is a PHP function, defined elsewhere, that returns a PDO object.
You of course do not have to use PDO at all, you can use the MySQL-
specific database functions if you like. But, assuming that you DO
/**
* Connect to django database.
* A PDO object
* PDOException - if connection error occurs
*/
function get_django() {
global $DJANGO_HOST, $DJANGO_NAME, $DJANGO_USER, $DJANGO_PW;
$dsn = sprintf("mysql:host=%s;dbname=%s", $DJANGO_HOST,
$DJANGO_NAME);
return new PDO($dsn, $DJANGO_USER, $DJANGO_PW);
}
--g
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users-***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
Shinmaikeru
2008-02-28 23:40:14 UTC
Permalink
Thanks for this thread and the solution. I too have a large PHP app
which is built on outdated ERW framework and has been heavily modified
and cludged so that it is like yarn and spaghetti mixed. I want to
redo it in Django, but there are so many custom reports and forms that
it will take a while, especially since I am just learning Django and
Python. If I can get away with making a base for the webapp which
incorporates the existing PHP reports and forms, I can then work
through them, replacing them with Django/Python code one by one.

Thanks again. I will look into this.
Post by rajiv bammi
Hey Gordyt,
Thanks a lot dear ..You made my day ;-) i thnk i got the solution ;-)
Thanks again..
Tk cr..
Byyee
Post by gordyt
In my case I am running a very current version of PHP and I have
packges installed that let me access my Django database. For this
particular app the DJango database is using MySQL and the get_djang()
line is a PHP function, defined elsewhere, that returns a PDO object.
You of course do not have to use PDO at all, you can use the MySQL-
specific database functions if you like. But, assuming that you DO
/**
* Connect to django database.
* A PDO object
* PDOException - if connection error occurs
*/
function get_django() {
global $DJANGO_HOST, $DJANGO_NAME, $DJANGO_USER, $DJANGO_PW;
$dsn = sprintf("mysql:host=%s;dbname=%s", $DJANGO_HOST,
$DJANGO_NAME);
return new PDO($dsn, $DJANGO_USER, $DJANGO_PW);
}
--g
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users-***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Loading...