Solved

Sessions using Dreamweaver

Posted on 2008-06-10
7
802 Views
Last Modified: 2013-12-13
I am developing a real estate search engine using primarily Dreamweaver for the search, results, and details page functionality. The site is working very slick and its time to add an "Add to Favorites / View Favorites" feature. When a consumer searches for properties, we want to give them the ability to add property detail pages to their list of favorites. When they leave the site and come back, we also would like them to be able to log in and view the list of favorites. I know this involves sessions. Where do I begin?
0
Comment
Question by:BMXPRO
  • 4
  • 3
7 Comments
 
LVL 31

Expert Comment

by:Frosty555
ID: 21755066
Provided that your PHP is setup to support sessions, it's pretty easy to use them.

Begin any page that will use sessions with the statement:
   session_start();

Now, you can access session variables via the $_SESSION[] array. Session variables will maintain their value inbetween page views for each user. Each user has their own unique session, and it expires when the user leaves the webpage or closes their browser.

For example, if on page1.php, you did:
   $_SESSION["test"] = "hello!";

And in page2.php, you did:
   echo $_SESSION["test"];

The user would view page 2 and see nothing. Then they would visit page 1. When they goes back to page 2, the session var is set and he would see "hello!" on the screen.

You can put an array in the session:
    $_SESSION["favorites"] = array();

and use push new items onto the array using array_push() to add a favorite. Later you can use a foreach{} loop to loop through all items in the array to list the favorites for the user.

Saving the favorites so the user can see them *after* they leave the page, and allow them to log back in later, will require a database (e.g. mysql), where you can store the favorites and user logins, and in that case sessions would only be used to keep track of the login name of the user visiting the page. The favorites would be stored in the database and accessed via a SQL query every time you need it. This also means it would be mandatory for the user to register a username and login before they can use the favorites feature.
0
 
LVL 31

Accepted Solution

by:
Frosty555 earned 500 total points
ID: 21755174
So... to do user logins and store favorites that way, you need databases as well as sessions, and things get a fair bit more complex. You'll need to identify what kind of database technology your webhost provides for you.

Various help topics from php.net to help you further:


Tutorial on sessions by W3Schools:
http://www.w3schools.com/PHP/php_sessions.asp

Tutorial on arrays by W3Schools:
http://www.w3schools.com/PHP/php_arrays.asp

Tutorial on using MySQL with PHP, again by W3Schools
http://www.w3schools.com/PHP/php_mysql_intro.asp


Session function reference:
http://ca.php.net/manual/en/ref.session.php

PHP Arrays reference:
http://ca.php.net/manual/en/language.types.array.php

Array_Push() function:
http://ca.php.net/manual/en/function.array-push.php

Foreach construct - for loop for looping through an array in PHP:
http://ca.php.net/manual/en/control-structures.foreach.php

0
 

Author Comment

by:BMXPRO
ID: 21755432
Frosty555,

We're using mySQL & PHP. Thank you so much for the tips. I have to leave the office now for a fews hours...will try some things upon return.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:BMXPRO
ID: 21762838
Frosty,
Do you have (or know of) any example code that accomplishes what I'm trying to do?
0
 
LVL 31

Expert Comment

by:Frosty555
ID: 21769381
There's quite a bit of sample code regarding how to use each function on PHP's website in the links I provided above. You will want to do some reading on the subject before you dive in. Those W3Schools links are excellent for getting started in the learning department.

But here's a simple script on how to setup logins using MySQL and PHP:
http://www.phpeasystep.com/workshopview.php?id=6

You can see it stores the logged in user's credentials in the session variables "myusername" and "mypassword".

Later you would need to create another MySQL table to hold the favourites, and use the myusername session var to select that one user's favourites list and display it on your page.
0
 

Author Comment

by:BMXPRO
ID: 21781767
Frosty555,
My deadline is quickly approaching. I'm trying to learn this in a very short time. Would you be available to help code the basic functionality of this?
0
 

Author Closing Comment

by:BMXPRO
ID: 31465930
Frosty555 helped me along with his tutorial links.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now