Solved

Sessions using Dreamweaver

Posted on 2008-06-10
7
804 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

776 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