?
Solved

Sessions using Dreamweaver

Posted on 2008-06-10
7
Medium Priority
?
827 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1500 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

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…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

771 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