mynamesleo Posted November 12, 2006 Share Posted November 12, 2006 Hi guys, new to php so go easy[code]<?phpif (isset($_COOKIE["TubeProgsVideo"])) { echo "Cookie is already set: "; echo $_COOKIE["TubeProgsVideo"];} else { [b]setcookie("TubeProgsVideo","New cookie", time()+60*60*24*90);[/b]} setcookie("TubeProgsVideo","akkksssssssss", time()+60*60*24*90);?>[/code]Ok so thats the code for test.php, if no cookie is set the last setcookie is set, which is great. But if the cookie is already set the last setcookie is ignored (I have changed the value, etc). What is the deal here? I tried deleting it first before setting it, no dice. I assumed cookie there or not, the last setcookie would simply overwrite it.Help! thanks! Quote Link to comment https://forums.phpfreaks.com/topic/27008-setcookie/ Share on other sites More sharing options...
printf Posted November 12, 2006 Share Posted November 12, 2006 cookie values are not tested, only cookie times (expires) are, so if you set the same cookie name with a new value, the second set-cookie will not overwrite the first, if the time is exactly the same! Because the script executes really fast, the time will not be different, so you need to add a second or more to the last cookie.Sonia Quote Link to comment https://forums.phpfreaks.com/topic/27008-setcookie/#findComment-123495 Share on other sites More sharing options...
mynamesleo Posted November 12, 2006 Author Share Posted November 12, 2006 Thanks for the replyI edited the time on the last setcookie and it still doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/27008-setcookie/#findComment-123506 Share on other sites More sharing options...
wildteen88 Posted November 12, 2006 Share Posted November 12, 2006 The way you have your code set up it will always create the TubeProgsVideo cookie which holds the value of "akkksssssssss" whether the first cookie is set or not. Quote Link to comment https://forums.phpfreaks.com/topic/27008-setcookie/#findComment-123511 Share on other sites More sharing options...
mynamesleo Posted November 12, 2006 Author Share Posted November 12, 2006 But the problem is, its not always doing it. It only works when there is no cookie there, then after that the last setcookie is ignored?!? Quote Link to comment https://forums.phpfreaks.com/topic/27008-setcookie/#findComment-123513 Share on other sites More sharing options...
mynamesleo Posted November 12, 2006 Author Share Posted November 12, 2006 Ok I have just been informed that you cant send anything out like echo "blahjh"; before attempting to set a cookie or it won't work, so i'll have a try at that Quote Link to comment https://forums.phpfreaks.com/topic/27008-setcookie/#findComment-123516 Share on other sites More sharing options...
mynamesleo Posted November 12, 2006 Author Share Posted November 12, 2006 [code]<?phpif (isset($_COOKIE["TubeProgsVideo"])) { //echo "Cookie is already set: "; //echo $_COOKIE["TubeProgsVideo"];} else { setcookie("TubeProgsVideo","New cookie", time()+60*60*24*90);} setcookie("TubeProgsVideo","Newuuuddddddddookie", time()+60*60*24*90);echo $_COOKIE["TubeProgsVideo"];?>[/code]Ok that works now, even with the same time? should I use a different time even though its working? or ? Quote Link to comment https://forums.phpfreaks.com/topic/27008-setcookie/#findComment-123518 Share on other sites More sharing options...
mynamesleo Posted November 12, 2006 Author Share Posted November 12, 2006 Ok this is driving me crazy now!Very first part of my code;[code]<?phpif (isset($_POST['submit'])) {setcookie("TubeProgsVideo","jllkkkkkkkiij", time()+60*60*24*90);[/code]Cookie was NOT set[code]<?phpsetcookie("TubeProgsVideo","jllkkkkkkkiij", time()+60*60*24*90);if (isset($_POST['submit'])) {[/code]Cookie WAS setwhy is [quote]if (isset($_POST['submit']))[/quote] { blocking it? Quote Link to comment https://forums.phpfreaks.com/topic/27008-setcookie/#findComment-123555 Share on other sites More sharing options...
wildteen88 Posted November 12, 2006 Share Posted November 12, 2006 Do you have a form field/button with the name as submit?If you don't then the if statement will return false and no cookie will get set. Post the code with your HTML here. Quote Link to comment https://forums.phpfreaks.com/topic/27008-setcookie/#findComment-123562 Share on other sites More sharing options...
mynamesleo Posted November 12, 2006 Author Share Posted November 12, 2006 The code is very messy at the moment, but it will give you the idea:[code]<?phpif (isset($_POST['submit'])) { setcookie("TubeProgsVideo",$video, time()+60*60*24*91); echo $video; $id = file_get_contents($video); $pos = strpos($id, "/player2.swf?video_id="); if ($pos === false) { echo " <span class='header'>YouTube video downloader</span><br /> <form method='post' action='index.php'> <input type='text' size='50' name='video' value='$link' class='btn' onclick=value=''> <input type='submit' value='Download' name='submit' class='btn'> </form> <font size='1' color='#FF2829' face='Tahoma'><b>Error! make sure the YouTube video link is correct</b><font><span class='default'><br />Eg: http://youtube.com/watch?v=DLWXyM5qWFo "; } else { $posT = strpos($id, "<title>YouTube - "); $posT = $posT + 16; $title = substr($id,$posT); $posT = strpos($title, '</title>'); $title = substr($title, 1, $posT-1); $pos = $pos + 21; $id = substr($id,$pos); $pos = strpos($id, '"'); $id = substr($id, 1, $pos-1); echo "<table border='0'><tr><td align='right'><font size='1' color='#000000' face='Tahoma'><b>$title</b></font><br /><br /><span class='default'><b>Help: </b>To watch downloaded YouTube videos you will need an FLV PLAYER, if you don't already have<br /> one you can download a good one <a href='http://applian.com/flvplayer/index_martijn.php' target='_blank'>HERE</a> or search google for a differernt one, theres lots around! </span><br /></td></tr><tr><td align='center'><form method='post' action='http://youtube.com/get_video.php?video_id=$id'><input type='submit' value='Download the video now!' name='submit' class='btn'></form></td></tr></table>";} } else {if (isset($_COOKIE["TubeProgsVideo"])) { $link = $_COOKIE["TubeProgsVideo"];} else { $link = "eee";}echo $_COOKIE["TubeProgsVideo"];echo "<span class='header'>YouTube video downloader</span><br /><form method='post' action='index.php'><input type='text' size='50' name='video' value='$link' class='btn' onclick=value=''><input type='submit' value='Download' name='submit' class='btn'></form>";}?>[/code]The plan is to plant the cookie of the value (Link) entered in the textbox after it has been submitted, so next time they visit the vaule in the textbox will be the last one they entered. I am still new with php so have to forgive the quality, etc. Quote Link to comment https://forums.phpfreaks.com/topic/27008-setcookie/#findComment-123565 Share on other sites More sharing options...
wildteen88 Posted November 12, 2006 Share Posted November 12, 2006 looking at your code. Your have not created the following variables when the form has been submitted:$video or $linkSo the cookie will not be set. What you'll want to do is add the following code:[code=php:0] $video = $_POST['video']; $link = $_POST['video'];[/code]After:[code=php:0]if (isset($_POST['submit'])) {[/code]Now your script should work correctly. Quote Link to comment https://forums.phpfreaks.com/topic/27008-setcookie/#findComment-123578 Share on other sites More sharing options...
mynamesleo Posted November 12, 2006 Author Share Posted November 12, 2006 Thankyou very much wildteen88 (and others!) it now works fine. 8) :o :o Quote Link to comment https://forums.phpfreaks.com/topic/27008-setcookie/#findComment-123581 Share on other sites More sharing options...
mynamesleo Posted November 12, 2006 Author Share Posted November 12, 2006 Ok well it was working now its not (i dont know what/if i changed anything)[code]<?phpif (isset($_POST['submit'])) { $video = $_POST['video']; $link = $_POST['video']; $id = file_get_contents($video); $pos = strpos($id, "/player2.swf?video_id=");if ($pos) { setcookie("TubeProgsVideo",$video, time()+60*60*24*90); $posT = strpos($id, "<title>YouTube - "); $posT = $posT + 16; $title = substr($id,$posT); $posT = strpos($title, '</title>'); $title = substr($title, 1, $posT-1); $pos = $pos + 21; $id = substr($id,$pos); $pos = strpos($id, '"'); $id = substr($id, 1, $pos-1); echo " <table border='0'><tr><td align='right'> <font size='1' color='#000000' face='Tahoma'><b>$title</b></font><br /><br /> <span class='default'><b>Help: </b>To watch downloaded YouTube videos you will need an FLV PLAYER, if you don't already have<br /> one you can download a good one <a href='http://applian.com/flvplayer/index_martijn.php' target='_blank'>HERE</a> or search google for a differernt one, theres lots around! </span><br /> </td></tr> <tr> <td align='center'> <form method='post' action='http://youtube.com/get_video.php?video_id=$id'><input type='submit' value='Download the video now!' name='submit' class='btn'></form> </td> </tr> </table> "; } else { echo "<font size='1' color='#FF2829' face='Tahoma'><b>Error! make sure the YouTube video link is correct</b><font><span class='default'><br />Eg: http://youtube.com/watch?v=DLWXyM5qWFo"; } } else { if (isset($_COOKIE["TubeProgsVideo"])) { $link = $_COOKIE["TubeProgsVideo"]; } else { $link = "Enter YouTube video url here and click download!"; }echo "<span class='header'>YouTube video downloader</span><br /><form method='post' action='index.php'><input type='text' size='50' name='video' value='$link' class='btn' onclick=value=''><input type='submit' value='Download' name='submit' class='btn'></form>";}?>[/code]Can you see anything wrong with that? Quote Link to comment https://forums.phpfreaks.com/topic/27008-setcookie/#findComment-123621 Share on other sites More sharing options...
wildteen88 Posted November 13, 2006 Share Posted November 13, 2006 It seems to be working fine for me. Whats it not doing with you and whats it supposed to do? Quote Link to comment https://forums.phpfreaks.com/topic/27008-setcookie/#findComment-124015 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.