PHP - Setting A Cookie
Hello,
My latest issue is with a login page I am trying to set a cookie for. I have deduced that it it pulling my info out of my database correctly. It does not give any errors for username or password when they are entered correctly but when I get to the end of the script where I would like to set a cookie and redirect to a new page it just stays on the same page and appears as if it is loading but nothing happens. After I stop the browser and attempt to reload the page it gives me an error saying the page cannot be displayed. Do I need to specify anything before I can have the following code work or is it that it is clashing somewhere else along the way? Code: [Select] <?php else { // if login good set cookie $_POST['username'] = stripslashes($_POST['username']); $hour = time() + 3600; setcookie(ID_my_site, $_POST['username'], $hour); setcookie(Key_my_site, $_POST['pass'], $hour); //then redirect them to the members area header("Location: http://example.com/"); die(); } ?> If I remove this code and simply leave the redirect as shown above, it works. But that defeats the purpose of the script as on the redirected page there has to be a cookie set to display it. Any help would be appreciated Similar TutorialsHi, I'm trying to set cookie but all in vain. I've already posted my php code in my last post. "COOKIE PROBLEM". I think there is nothing wrong with code. may be i need to enable to load some library or something in xampp. Is there anyone who knows what setting do i need to do for enable cookies and in which file? Please help I have a web application in which there are two php files index.php and include.php . include.php is loaded to a DIV tag inside index.php on click of button using Jquery . Both these php files share a common varible through cookie which is used to keep track of the data retrived by include.php . This variable will be updated each time include.php retrives data . These php code work fine Mozilla and chrome but not in IE . When i checked the cookie value it is same through out without setting . Please help . Thanks in advance , Rahul Here is my code that sets the cookie for a year: //set a cookie for a year setcookie('user', $username, time()+31536000); My code that unsets it: <?php unset($_COOKIE['user']); header("LOCATION:index.php"); ?> Why doesn't it log the user out? Hi, I'm pretty new to php, and to this site as well, so please bear with me... I have a table in my database which has "user_id" as its primary key, and i want to set the values under this as a cookie. I've put in bold the main things that are affecting it (I think). As is, the mysite_userid cookie does not set properly, and I get an error like this "Header may not contain more than a single header, new line detected...." Any help would be really appreciated!! <?php include("connect.php"); $match = "select user_id from $table where username = '".$_POST['username']."' and password = '".$_POST['password']."';"; $qry = mysql_query($match) or die ("Could not match data because ".mysql_error()); $num_rows = mysql_num_rows($qry); if ($num_rows <= 0) { echo "Sorry, there is no username ".$_POST['username']." with the specified password. "; echo "Try again"; exit; } $remember = strip_tags($_POST['remember']); if ($remember) { setcookie("loggedin", "TRUE", time()+3600*24); setcookie("mysite_username", "".$_POST['username'].""); header("Location: members.php"); } else { setcookie("loggedin", "TRUE"); setcookie("mysite_username", "".$_POST['username'].""); setcookie("mysite_userid", $qry); $site_username = $HTTP_COOKIE_VARS["mysite_username"]; $site_userid = $HTTP_COOKIE_VARS["mysite_userid"]; header("Location: members.php?user=$site_userid"); } ?> hi today i was checking a site as i m learning more about curl. i see i was not able to get image with curl. they have stopped hotlinking and was not finiding anyway to show me image on my localhost so making this thread for any pro help. here is my code: $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://www.pixhost.org/show/2019/5530476_banner.png"); curl_setopt($ch, CURLOPT_USERAGENT, "Opera/9.80 (Windows NT 5.1; U; en) Presto/2.5.24 Version/10.54"); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_COOKIE, 'pixhostads=1'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec ($ch); echo $result ; if i run that script it will show me that page but it says pictures is hotlinked and cant display, so i tried to see cookie but i m not sure if i am doing it right. on that url u must need to click "Click to continue" before u can see image. Please can someone help why its not showing me the correct image and any way to pass it? thanks Hi girls and boys I am trying to set a variable if a session OR a cookie has been set, but am unsure on how to write the statement... if (isset($_SESSION['name'])||isset($_COOKIE['name'])) {$variable = $_SESSION['name']||$_COOKIE['name'];} Obviously not working there, but just need a pointer here. any help is appreciated... Hi, probably a simple solution to this which I am overlooking, doing my first foray into using cookies. So far all is well, but how do I make a cookie valid for both www.mysite.com and mysite.com Code: [Select] if(isset($_POST['remember'][1])) { $session_id = md5($_SESSION['username'] . $_SESSION['last_login']); setcookie("session_id", $session_id, time()+60*60*24*30, "/"); } Thanks, TS Ok I have my login form and previously I was using $_SESSION untill I learned this removes data when browser is closed. Someone said to me to use cookies but I have no clue where to begin on setting a cookie or reading information from it. Can anyone point me to a tutorial on cookies? I have part of my script that I have used many times in many diffrent applications that works just fine. But in this use it refuses to set the cookie or do anything besides the redirect for that matter. // if login is ok then we add a cookie $ip = $_SERVER['REMOTE_ADDR']; $datem = date("j F Y, g:i a"); mysql_query("UPDATE YBK_Login SET date = '$datem' AND ip = '$ip' WHERE ID = '{{$info['ID']}'")or die(mysql_error()); $_POST['username'] = stripslashes($_POST['username']); $hour = time() + 3600; setcookie('ID_WatsonN', $_POST['username'], 0); setcookie('Key_WatsonN', $_POST['pass'], 0); setcookie('UID_WatsonN', $info['ID'], 0); setcookie('LOGIN', $info['ID'], time()+3); //then redirect them to the members area Header("Location: dashboard.php"); Hi, I was trying to use setcookie on my website but when I try to use, it wasn't setting anything and then I tried to make sure if it's setting something, I added Code: [Select] echo $_COOKIE;But it shows Array (just the word) instead of tblogvalue. This is the code I'm using; Code: [Select] $Month = 2592000 + time(); setcookie(tblog, tblogvalue, $Month); echo $_COOKIE; I have a question about cookies I want the cookie to work on all the paths / folders / directorys located in a root path. So say like.. http://www.site.com/members/login.php is the folder which sets the cookie, then this cookie will also work on like.. http://www.site.com/bla/boo/works.php I'm trying to do: setcookie(Data1, 'valueofthecookie', '/', '.site.com', 1); But its not working... if a user clicks ?hide=1 or ?hide=2 it does Code: [Select] if (isset($_GET['hide'])){ $id = intval($_GET['hide']); setcookie('hide', ''.$id.'',time()+32000000); header('Location: index.php'); exit; } How do I make it so if they click ?hide=1 it ad's 1, but what if they do ?hide=2 also? it would need to be 1,2 not just 2. i want to store the nickname of the user as a cookie.. somthing like set cookie name=nickname,value=smarty,expires after 1 week i know cookies are stored against domain names. what happens when another person logs in the same computer, with different login, and his nickname is diff how does the cookie get handled, and how does the right nickname flash against the right persons login id... Hey PHPFreaks! I have a problem with my login script, because when i login it sets a cookie and it all works. But when i got redirected and refresh the page, my script turns an error which says i'm not logged in? how come? This is what finds the cookie and redirects me Code: [Select] if(!isset($_SESSION['auth']) && !isset($_COOKIE['authcookie'])) { $_SESSION['ERROR'] = 7; header('location: index.php?login'); } how can i set cookie like a*dm*n@**t*r*c**.*c** ?? Code: [Select] $cookkiee = $islem_1['mail'] ; $sayisi = strlen($cookkiee)-1 ; $cok = $islem_1['mail'] ; for($i=0;$i<=$sayisi;$i++) { $sifr = rand(0,2); if($sifr ==0) { $cok[$i]= '*'; } $dizik = $cok[$i]; setrawcookie("kayip",$dizik,time()+(60*30),"/"); $_COOKIE['kayip'] = $dizik; echo $dizi // etc . a*dm*n@**t*r*c**.*c** } echo $_COOKIE['kayip']; // etc. just one char being like random a Hi all, I'm struggling with a program I'm trying to write with cookies, so any help would be very much appreciated!! What I'm wanting to do is when someone visits my site, I want to display content until the end of the day, and once the days up it disappears forever/until the cookies are deleted. Could someone help me with this? Thanks lots in advance, Jake How to get the cookie value,when we login for a website,also how to access the site by using the cookie values? Hi again all, I wondered if anyone could help again (sorry). I have one single page which I would like to record a cookie when the user hits submit. The next time the user visits the site, I want it to recall the cookie and put the value in the textbox on the page. I think I have it storing the cookie (not sure though), but I am having trouble getting it to recall in the text box. I wondered if someone could look at this code and see where I am going wrong or help with what I need to add. New to PHP so having trouble getting my head around things. Thanks in advance. Code: [Select] <? setcookie('user',$userName,(time()+60*60*24*30) ); ?> <style type="text/css"> <!-- body,td,th { color: #FFFFFF; } body { background-color: #000000; } .style1 {font-family: Verdana, Arial, Helvetica, sans-serif} --> </style><form id="form1" name="form1" method="post" action=""> <label> <div align="center" class="style1"><strong>My Timetable Search</strong><br /> <br /> Enter Network User ID:<br /> <input type="text" name="txtUserID" id="txtUserID" value=""/> </div> <p align="center"> <label> <input type="submit" name="cmdGo" id="cmdGo" value="Show Timetable" onclick="setcookie('user',$userName,(time()+60*60*24*30);"/> </label> <br /> </p> <hr /> </form> <div align="center"> <?php $userName = $_POST["txtUserID"]; $hostname = "xxxxx"; $dbuser = "xxxxx"; $dbpassword = "xxxxxx"; $dbname = "xxxxx"; $LessonCount = 0; $db_link=mysql_connect($hostname, $dbuser, $dbpassword) or die("Unable to connect to the server!"); mysql_select_db($dbname) or die("Unable to connect to the database"); $fields_array=array(); $num_fields=0; $num_row=0; $sql= "SELECT core.UserID, wstt.Period, wstt.Lesson, wstt.Staff, wstt.Room FROM wstt INNER JOIN core ON wstt.Adno = core.Adno WHERE core.UserID='$userName'"; // find position of "FROM" in query $fpos=strpos($sql, 'from'); // get string starting from the first word after "FROM" $strfrom=substr($sql, $fpos+5, 50); // Find position of the first space after the first word in the string $Opos=strpos($strfrom,' '); //Get table name. If query pull data from more then one table only first table name will be read. $table=substr($strfrom, 0,$Opos); // Get result from query $result=mysql_query($sql); $num_row=mysql_numrows($result); print('<html>'); print('<head><title>'); print('Timetable for'.$userName.'</title>'); print('<link rel="stylesheet" href="style.css">'); print("</head>"); print('<body><br>'); print('<h3>Timetable for: '.$userName.'</h3>'); print('<div align="center"'); if($num_row >0) { //Get number of fields in query $num_fields=mysql_num_fields($result); # get column metadata $i = 1; //Set table width 10% for each column $width=10 * $num_fields; // print('<br><table width='.$width.'% align="center" border=1> padding=0<tr>'); print('<br><table width='.$width.'% border="1" cellpadding="0" cellspacing="0" bordercolor="#000066"<tr>'); print('<tr><th colspan='.$num_fields.'>Timetable List</th></tr>'); while ($i < $num_fields) { //Get fields (columns) names $meta = mysql_fetch_field($result); $fields_array[]=$meta->name; //Display column headers in upper case print('<th><b>'.strtoupper($fields_array[$i]).'</b></th>'); $i=$i+1; } print('</tr>'); //Get values for each row and column while($row=mysql_fetch_row($result)) { print('<tr>'); //SET TO 1 TO HIDE USERNAME for($i=1; $i<$num_fields; $i++) { //Display values for each row and column print('<td>'.$row[$i].'</td>'); } //START if ($LessonCount ==4) { print('<tr>'); print('<td colspan="4"><div align="center">-----------------------------------------------------</td></tr>'); $LessonCount=0; }else { ($LessonCount = $LessonCount+1); } //STOP print('</tr>'); } } print('</div>'); ?> </div> I see all over the web that I should tie a session cookie to an ip to help stop some XSS session stealing, but I can't find HOW to do this anywhere. Can someone post some example code? Thanks! test.php Code: [Select] <?php setcookie("test", 'tester', time()+3600*24*30 , "/", ".mystagingsite1.com"); header('Location: test2.php'); ?> test2.php Code: [Select] <?php echo '<pre>'; print_r($_COOKIE); echo '</pre>'; ?> This does not work. It's not setting the cookie at all. Is there something I am doing wrong here? |