Solved

php postgresql login script help

Posted on 2012-03-12
9
1,805 Views
Last Modified: 2012-04-09
What are the steps to make a simple login? I've found several scripts for php/mysql; I happen to be using postgresql, and thought it may be easier just to write it myself since the free content that's available written for postgresql is a little on the light side.

The script doesn't immediately need many of the features that the more professional, fleshed-out mysql scripts include. The number of users is so small right now, that I'll be issuing usernames and passwords (I don't think the functionality to add accounts dynamically will be too difficult to add later).

So given all that, how is it best to save to a user's computer that he or she is logged in? Do I simply give him a cookie logged_in and set the value to true? Do I save the ip of the "logged in computer" to the database and remember it that way. Many of these php/mysql scripts seem to use $_SESSION variables as well; do I need to use those somehow?

I realize this is a pretty vague question, I'm just sort of brainstorming exactly what I need to do.

Or, if anyone's aware of a good postgresql login script, I'd be happy to explore that as well ;)

Thanks!
0
Comment
Question by:hibbsusan
  • 5
  • 2
  • 2
9 Comments
 

Author Comment

by:hibbsusan
ID: 37712405
Or, since I have my users table in postgresql and there are plenty of good php/mysql login  scripts out there, is there some way I can just mirror that users table in mysql so I can use one of the open source login scripts, accessing the users through mysql..?
0
 
LVL 6

Expert Comment

by:Patrick Tallarico
ID: 37712430
You should be able to modify the login script of your choice to look at the postgresql database rather than a mysql db.  The php functions for postgresql are for the most part the same as mysql, with the exception that they are prefixed with 'pg_...' rather than 'mysql_...'
You would also have to check the syntax on the mysql queries just to make sure that the slight differences in syntax are accounted for.

As for synchronizing between pg, and mysql, I have used Php to synchronize data between mssql and mysql.  Pull the data from one server into an array, and loop through the rows, creating insert queries, or importing the array into a temp table and running an update to another local table...

If you attach a sample of code that makes sense to you, I'm sure we can work it to fit your needs.
0
 

Author Comment

by:hibbsusan
ID: 37712452
I have used this login script before, but I don't know if updating it will be as simple for me. It uses a lot of php classes that I don't fully understand the functionality of. So I don't know if I can just do a replace all mysql¿pg.

I think the way php connects to the database differs as well. Maybe you can take a look and let me know how feasible you think it will be... :)

Thanks for your help!
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:hibbsusan
ID: 37712469
that link was incorrect. This is the right one: http://www.html-form-guide.com/php-form/php-login-form.html
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 37712677
This article describes the design pattern.  It should be readily adaptable for PostGreSQL.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_2391-PHP-login-logout-and-easy-access-control.html

After you have read it over please post back with any specific questions.  Best, ~Ray
0
 
LVL 6

Expert Comment

by:Patrick Tallarico
ID: 37715513
There is only one file that has actual database calls in it.   Tha is the file includes/fg_membersite.php

In there there are a few different mysql functions being used, all of which have a direct correlating function in pg.  The one that is most different is the mysql_connect, and mysql_select_db functions in the php function DBLogin().  All of the options that are handled in this php function can be handled in the pg_connect() function.  pg_connect() uses a single connection string argument rather than the three arguments for the mysql_connect().
what you would look to use would be something like:
pg_connect("host=localhost port=5432 dbname=test user=name password=pass options='--client_encoding=UTF8'")
This would actually take care of all of DBLogin() options in one single call.

The rest of the file has a few more mysql calls, they relate to pg as follows:

mysql_query($query,$connection)  ==> pg_query($connection,$query)
mysql_num_rows($result)  ==>  pg_num_rows($result)
mysql_fetch_assoc($result) ==>  pg_fetch_assoc($result)
mysql_real_escape_string($string) ==>  pg_escape_string($string)
mysql_error()  ==>  pg_last_error($connection)

Note that the query arguments are reversed in order from one to the other.  Also I am not sure that the pg_last_error will be exactly the same as the mysql_error in what it returns, but it will give you something close enough that the program should work.

By adjusting this file, you should be able to use this same login program with a pg database as you would have with a mysql.  Note you will still need to adjust the membersite_config.php and adjust the requisite user table in pg.  In terms of feasibility, I think it shouldn't be too difficult.

There are a lot of functions that this script creates and uses.  Unfortunately I don't have the time to go over them with a fine tooth comb, but it seems to be pretty well designed and organized.  

If you are unsure of what all is going on with this program you may wish to go with something a bit simpler, or at least something that you write yourself.  There are some decent tutorials out there.  A good reference is the article that Ray_Paseur posted. You may be able to utilize that to create something for yourself so that you have exactly what you want rather than this other code.
0
 

Author Comment

by:hibbsusan
ID: 37720159
Mr Paseur,
I think this article is really great. I am still working through it. Thank you so much for writing it and pointing me to it.
0
 

Author Closing Comment

by:hibbsusan
ID: 37825197
Hi Ray,

I was daunted by making this work for postgresql for quite a while, but I finally devoted some time, and it is now working very well.

Thanks!
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 37825597
Thanks for the points.  I've done a little work with PostGreSQL and found it to "be SQL" in much the same way that other SQL data bases are alike.  What I like better about MySQL is the more robust collection of helpers.  phpMyAdmin is much better than phpPGAdmin (or whatever it's called nowadays).  But the principles of SQL seem to come through.  I'm glad they came through for you!  All the best, ~Ray
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
The viewer will learn how to count occurrences of each item in an array.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question