Solved

Sessions using Dreamweaver

Posted on 2008-06-10
7
803 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

920 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

16 Experts available now in Live!

Get 1:1 Help Now