TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 04-06-2010, 03:41 PM   #1 (permalink)
The Contributor
 
Tim Dobson's Avatar
 
Join Date: Feb 2010
Posts: 69
Thanks: 16
Tim Dobson is on a distinguished road
Default Working with cookies ARRG

Ok so i have 2 pages one where the user can view it as a slide show and one where they can view it as a list using <table>

So i come to the idea of using the list incase the user does not have high speed net and thought it might take them a while to actually load the slide... so they have the option just to view it as a list. So upon this i was thinking to myself well if they want to view it as a list then i need it to go back to the list when they re-visit the site rather than going to the default page (where the slide show is) so i thought cookies would do the trick!

So here i have it

index.php containing:
PHP Code:
<?php
if(isset($_COOKIE['viewinglist']))
    
header'Location: indexlist.php' ) ;
else

?>
If there is a cookie there then redirect them to indexlist.

So now in indexlist.php i got:
PHP Code:
//Calculate 60 days in the future
//seconds * minutes * hours * days + current time
$inTwoMonths 60 60 24 60 time(); 
$value "Viewing as list";
setcookie('viewinglist'$value$inTwoMonths); 
?> 
We are setting the cookie so they have entered the page so when it goes to index it will read this cookie to show they should be on indexlist.php and it will redirect them to there.

Ok... so now, the user has just clicked the view as slide show!
this will take the user to:
PHP Code:
includes/viewasslide.php 
which contains code to "delete" the cookie or if you like "set it to a date before the now time...
PHP Code:
<?php
//Killing the cookie:
$cookie_name="viewinglist";

//here we assign a "0" value to the cookie, i.e. disabling the cookie:
$cookie_value="";

//When deleting a cookie you should assure that the expiration date is in the past,
//to trigger the removal mechanism in your browser.
$cookie_expire=time()-60;

$cookie_domain="";
setcookie($cookie_name$cookie_value$cookie_expire"/"$cookie_domain,0);

//re-direct to login screen (or any other you like):
header"Location:../index.php");
exit;
?>
Now the problem is when i click this link and run this script its not actually deleting it or resetting the value to a previous time and keeps taking me back to indexlist.php where really it should be on index.php.

I cant see myself where its wrong but it obviously is. Could someone point out where im wrong? Thanks
Tim Dobson is offline  
Reply With Quote
Old 04-06-2010, 06:03 PM   #2 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Just out of curiousity, why all the redirects? Without being completely familiar with the logic operating here, it seems like you're sending the user all over the board just to define a setting for the current page.

Use sessions. Set them in the head of the script, and use the values of these to determine how the rest of the script will display its content.
delayedinsanity is offline  
Reply With Quote
Old 04-06-2010, 06:46 PM   #3 (permalink)
The Contributor
 
Tim Dobson's Avatar
 
Join Date: Feb 2010
Posts: 69
Thanks: 16
Tim Dobson is on a distinguished road
Default

No they only get sent to 2 places. Indexlist.php when they click on view as list and index.php when they click on view as slide.
Tim Dobson is offline  
Reply With Quote
Old 04-06-2010, 07:37 PM   #4 (permalink)
The Contributor
 
Tim Dobson's Avatar
 
Join Date: Feb 2010
Posts: 69
Thanks: 16
Tim Dobson is on a distinguished road
Default

Well taking a look at the sessions. This is not what i want, this is temporay data, i need it as perminant data so if the users closes the browser and returns say 5 days later when he visits he will be back at the page he was viewing 5 days previous.
Tim Dobson is offline  
Reply With Quote
Old 04-06-2010, 08:59 PM   #5 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

That's understandable - but I'm still not quite following the logic of your redirects, and this could be as simple as my thought process not coinciding with yours on how to do such things.

1. Check for existence of cookie, 'yoursite_view' and retrieve value of said cookie
- set a cookie if none exists and we need to
- assign value of cookie or result of user interaction to a local var we can use later
2. Use value of local var to determine what page style we're looking for
3. Load that page style inline, without redirecting.

The idea here is to do everything we need to encapsulated inside the single page that's going to be presenting the results. Include your functions from a remote file if you like, but keep it silent so that all the user knows is that they're getting the view that they want, on the page that they were visiting.
delayedinsanity is offline  
Reply With Quote
Old 04-07-2010, 09:30 PM   #6 (permalink)
The Contributor
 
Tim Dobson's Avatar
 
Join Date: Feb 2010
Posts: 69
Thanks: 16
Tim Dobson is on a distinguished road
Default

Ok i will try explain it more to show you what im trying to accomplish. If i give you the url to my site it may be a big more clear (http://www.tentun.co.uk/sb0t/)

You can see on the index page there is a slideshow, this will allow users to view each template on the site or as more view each preview and have a download link on each slide.

Now i have the problem where some people may not be able to have the slide show, or it takes a long time for there browser to load it due to maybe slow connection? Upon thinking this i thought welll let them have the option to view it as a list in a table.

Now that slide show is on another page just so it wouldent have so much jibberish in the index... so i used the include.

My original idea was just to have it load a different include using the if statement if the user choose this option, but i got a bit stuck doin that like i got a bit stuck doing this lol.

So i created another page with the list on it, almost exactly the same as the index but without the slide show, so i created a url on the top to "view as list" which would take them to the list page... on the list page there is a link "view as show" which would take them back to the main index. So really if they choose the "show as list" i was trying to just set a cookie so when they return to the site in the future rather than taking them to the index it should take them to the list, or if they clicked another link on the site and then clicked the link "Template directory" if they were originaly viewing it as list rather than take them to the index like it does by default it would take them to the "list" page.

This was the idea behinde just using cookies, there is an if statement and i used it to see if there was a cookie there and if there is a cookie there it would take them to the list page rather than the index, and the "view as slide" link would or should make that cookie invalid, which is where i am getting stuck because i dont know how to configure it correct. My guess correct me if im wrong is because maybe i need to use the value of the cookie? but again i dont know how to go about this and use an if statement to determine which page they should be at.
Tim Dobson is offline  
Reply With Quote
Old 04-07-2010, 09:30 PM   #7 (permalink)
The Contributor
 
Tim Dobson's Avatar
 
Join Date: Feb 2010
Posts: 69
Thanks: 16
Tim Dobson is on a distinguished road
Default

jesus i just wrote an essay haha sorry about it being so long just trying to shed more light on what im trying to do :P
Tim Dobson is offline  
Reply With Quote
Old 04-08-2010, 01:31 AM   #8 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

No more redirects! :) Tell me if the following makes sense:

php Code:
// Set default values
$default_view  = 'gallery';
$cid           = 'tentun_view';
$expire        = strtotime( '+2 Months' );

// Look for the cookie, and validate its value or set our view to the default
$view = ( isset( $_COOKIE[$cid] ) && in_array( $_COOKIE[$cid], array( 'gallery', 'list' ) ) ) ? $_COOKIE[$cid] : $default_view;

// Allow the view to be short circuited via GET
if ( isset( $_GET['view'] ) )
    $view = ( $_GET['view'] != 'list' ) ? $default_view : 'list';

// Remember the desired view
// You could remove the if statement and set it on every page view to constantly push the expire forward
if ( ! isset( $_COOKIE[$cid] ) || $_COOKIE[$cid] != $view )
    setcookie( $cid, $view, $expire, '/', '.tentun.co.uk' );

echo $header; // This stuff is up to you of course

// Set PATH and include a file named after your view
require_once( PATH . "{$view}.php" );

echo $footer;

Last edited by delayedinsanity : 04-08-2010 at 03:20 PM.
delayedinsanity is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Remember me login w/ cookies Killswitch General 5 12-24-2009 09:54 PM
Wamp working, but what next? Help! DotNetTim Absolute Beginners 3 02-07-2009 09:05 AM
Mod Rewrite only partially working Gareth General 6 12-24-2008 05:08 PM
function that returns an array -- why not working? Dave Absolute Beginners 3 08-13-2008 05:01 PM
working with Iframes in php JMHein Absolute Beginners 3 05-17-2008 03:40 PM


All times are GMT. The time now is 09:45 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design