hi guys
i am working on a site where people can add a button to add a search listing to their favorite item using cookie in php. then they can get this list of cookies to email to their friend, now is there a way to do this? I mean how can we do this without using database?

many thanks in advance.
:-)

the only way i can see that is either to set all data in cookies so there favorite items

setcookie("user", $user, time()+3600);
setcookie($user."fav1", "fav 1", time()+3600);
setcookie($user."fav2", "fav 2", time()+3600);
setcookie($user."fav3", "fav 3", time()+3600);
setcookie($user."fav4", "fav 4", time()+3600);

so then when the user is on your website you can load

$usercookie = $_COOKIE["user"];
echo $_COOKIE[$user."fav1"];
echo $_COOKIE[$user."fav2"];
echo $_COOKIE[$user."fav3"];
echo $_COOKIE[$user."fav4"];

another way to do it is to have php write a file if doing a file per user it would be something like this

// this writes to a file
$myfavfile = $user.".txt";
$fh = fopen($myfavfile, 'w') or die("can't open file");
$stringData = "my favorite\n";
fwrite($fh, $stringData);
fclose($fh);

// to read from the file
$usercookie = $_COOKIE["user"];
$myfavs = $usercookie.".txt";
$fh = fopen($myfavs, 'r');
$thefavorites = fread($fh, filesize($myfavs));
fclose($fh);
echo $thefavorites;

you could also take the second example and make a file for everyone and then make a script that would open the file up and load into arrays so you can use the array search function and it would tell you where the items are for that user

im not sure if these are the best examples but i just through them together on the fly

thanks for the reply, i liked option two as its mobile based site and only mobile user is going to access the favorite list. so is this also possible even without logging in?
and if yes, how can i get list of all the favorite item to send by email to check or unchecked from the fav list , which one to mail and which not to.

thanks.

yes it is possible without a login only issue is how are you going to track which list is for what user you can set it ip address but if the mobile device changes ip's its going to not be there

$ip = $_SERVER['REMOTE_ADDR']; 
// then you can write to a file and either name it with the ip address or use the ip address as you identifier

as for the second part yes also once you read the file you can make a form using the items you read from the file then have the user check off the ones you don't want and then email and rewrite the file if you want to delete them if you want i can probley whip up a example of it

Hi
thanks for reply again,it would really be great if you could illustrate by an example, how can i create cookie and store and retrieve them , like for "favorite item 1", "favorite item 2"," favorite item 3.."etc .. cos once user will click on an item in the search listing on his mobile, it will be added to favorite using cookie then he will favorite second item and that also will be added to cookie and then he will press a link to retrieve those list of all cookies and send that list using email , rest after fetching those list i can manage how to email that listing of cookies, sorry but i m a but new with all this :-) thanks for help

ok ill write some thing up

you said option two so this is using writing to a file meathod

<?php
 
 //================================================= filename is ip with no dots =================================
// get users ip address
$ip=$_SERVER['REMOTE_ADDR'];


//make ipaddress filename just get rid of the .'s'
$ip = preg_replace("/[^a-zA-Z0-9\s]/", "", $ip);





//================================================= links  ==========================================================
// this is just a link it makes you add a cookie via url
echo "<a href=\"?addcookie=somecookie\" target=\"_self\">ADD A COOKIE</a>";

echo "</br></br>";

// this is just a link it makes you add a cookie via url
echo "<a href=\"?email=yes\" target=\"_self\">EMAIL LIST</a>";






//================================================= if user click link ==============================================
//if link is pressed and add cookie is not empty
if($_GET['addcookie']){
	
	
//check to see if a file exists already	
	if(file_exists($ip.".txt"))
	{

$fh = fopen($ip.".txt", 'a') or die("can't open file");
fwrite($fh, $_GET['addcookie']);
fclose($fh);
	
	}else{		
// if the file doesnt exist 
$fp = fopen($ip.".txt", "w");
fwrite($fp,"\n\n".$_GET['addcookie']."\n\n");
fclose($fp);
	}
	
	echo "cookie added"	;

}



//================================================= email form ====================================================

if($_GET['email'] == "yes"){
	echo "<form action=\"?email=send\" method=\"post\" target=\"_self\">
	PLEASE PROVIDE EMAIL</br><input name='emailsend' type=\"text\" /></br><input type=\"submit\" />
</form>";
	
}


//================================================= send email ====================================================
if($_GET['email'] == "send"){
	
// read file

$fh = fopen($ip.".txt", 'r');
$message = fread($fh, filesize($ip.".txt"));
$message = addslashes($message);
// send email	
$to = $_POST['emailsend'];
$subject = "cookies fav list yea";
$from = "someonelse@example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
fclose($fh);
echo "Mail Sent.";
	
}

?>

thanks for share, hitman.

your welcome

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.