PHP - Moved: Cookie Help?
This topic has been moved to Miscellaneous.
http://www.phpfreaks.com/forums/index.php?topic=346675.0 Similar TutorialsThis topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=349632.0 This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=306496.0 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... 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... 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 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 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. 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 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"); 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? 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'); } 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! Hello all, I am extremely new with php, I just started learning it this week. I am tryin to make a secure login page that uses cookies for authentication. The problem I am having is that I cannot seem to get it to detect or read the set cookie properly. I want it to detect if its the correct username in the cookie and if so, allow to see the page, and if now, then return to the login screen. The login screen is login.html, which directs the person to the php script login.php. That should in turn show them a message page and some short info, as well as a link to their control panel (index.php) . The problem is that even if i skip the login I can still reach the control panel (index.php) with or without the cookie. Here is my code (please be gentle this is my first week with php): login.html: Code: [Select] <html> <head> <Title>Admin Login</Title> </head> <body> <center> <br> <br> <br> <br> <img src="pk.png"> <form action="login.php" method="post"> <br> Username: <input type="text" name="username" /><br> Password: <input type="text" name="password" /><br> <input type="submit" value="Login" /> </form> </center> </body> </html> login.php: Code: [Select] <?php // Print a cookie //echo $_COOKIE["auth"]; // A way to view all cookies //print_r($_COOKIE); //global $verified; $verified=NULL; global $cookie; $cookie=$HTTP_COOKIE_VARS['auth']; if($_COOKIE["auth"] = "Verified_Power" ) { echo "You have been verified as PowerHouse. <br><br>"; $verified="TRUE"; } else if($_COOKIE["auth"] = "Verified_Thor") { echo "it's actually thor!<br><br>"; $verified="FALSE"; } else { echo "Bad Chookie"; $verified="FALSE"; exit(); } ?> <html> <head> <Title>Login Info</Title> </head> <body> <center> <img src="pk.png"> <br> <br> <br> Hello <?php echo($username); ?>!<br /> <?php //header("Cache-Control:no-cache"); $msgfile = "messages.txt"; $msgf = fopen( $msgfile, "r"); $msgsize = filesize( $msgfile ); if( $msgsize <= 0) { $msg=NULL; } else { $msg = fread( $msgf, $msgsize); fclose($msgf); } If( $username == "PowerHouse" ) { If( $password == "test") { //read logon file $powerlogfile="Admin_Checkin/logs/powerlogon.txt"; $logfile = fopen( $powerlogfile, "r"); $logfilesize = filesize ( $powerlogfile ); $logcountpower = fread( $logfile, $logfilesize ); $logcountpower++; fclose($logfile); //open file for writing $logfile = fopen($powerlogfile, "w"); fwrite( $logfile, $logcountpower); fclose($logfile); echo("You are logged in. <br><br>"); echo("It is you master! <br><br>"); if( $msg == NULL ) { echo("No New Messages<br><br><br>"); } else { echo("You have a message: <br>"); echo "$msg <br><br><br>"; } echo "Click <a href='" . "/Admin_Checkin/14795" . "'>Here</a> To access your control panel.<br><br><br>"; echo "You have logged in $logcountpower times."; //open logfile to write to $logfile=fopen("Admin_Checkin/logs/powerlog.html", "a"); //write the time of access $time=date("H:i:s: dS F"); fwrite($logfile, "<b>Time of access:</b> $time<br>"); //write users ip if( $REMOTE_ADDR != NULL ) { fwrite($logfile, "<b>IP Address:</b> $REMOTE_ADDR <br>"); } //write users forwarding url if( $HTTP_REFERER != NULL) { fwrite($logfile, "<b>Referer:</b> $HTTP_REFERER <br>"); } //write users browser info fwrite($logfile, "<b>Browser Info:</b> $HTTP_USER_AGENT <hr><br>"); setcookie("auth","Verified_Power", time()+3600); //header("Location:login.php"); exit(); //setcookie('login', $_REQUEST['username'].','.md5($_REQUEST['username'].$secret_word)); } else if( $password != "test" ) { $pwfail++; echo("<hr>You have entered the wrong password, PowerHouse. <br>"); } } else if($username == "ThorSummoner") { If( $password == "test") { //read logon file $thorlogfile="Admin_Checkin/logs/thorlogon.txt"; $logfile = fopen( $thorlogfile, "r"); $logfilesize = filesize ( $thorlogfile ); $logcountthor = fread( $logfile, $logfilesize ); $logcountthor++; fclose($logfile); //open file for writing $logfile = fopen($thorlogfile, "w"); fwrite( $logfile, $logcountthor); fclose($logfile); echo("You are logged in. <br><br>"); echo("This is the Admin Portal Welcome Screen. <br><br>"); if( $logcountthor == 1) { echo "This is your first visit, yay! <br><br>"; } //echo "$msg <br> <br>"; if( $msg == NULL ) { echo("No New Messages<br><br><br>"); } else { echo("You have a message: <br>"); echo "$msg <br><br><br>"; } echo "Click <a href='" . "/Admin_Checkin/atfg4gc" . "'>Here</a> To access your control panel.<br><br><br>"; echo "You have logged in $logcountthor times."; //open logfile to write to $logfile=fopen("Admin_Checkin/logs/thorlog.html", "a"); //write the time of access $time=date("H:i:s: dS F"); fwrite($logfile, "<b>Time of access:</b> $time<br>"); //write users ip if( $REMOTE_ADDR != NULL ) { fwrite($logfile, "<b>IP Address:</b> $REMOTE_ADDR <br>"); } //write users forwarding url if( $HTTP_REFERER != NULL) { fwrite($logfile, "<b>Referer:</b> $HTTP_REFERER <br>"); } //write users browser info fwrite($logfile, "<b>Browser Info:</b> $HTTP_USER_AGENT <hr><br>"); setcookie("auth","Verified_Thor", time()+3600); } else if( $password != "test" ) { $pwfail++; echo("You have entered the wrong password, ThorSummoner. <br>"); } } else if($username !== "PowerHouse" && $username !=="ThorSummoner") { echo("Who are you?"); } ?> </center> </body> </html> index.php Code: [Select] <?php global $verified; echo ($verified); If( $verified == "TRUE" ) { echo "You are verified"; } else if( $verified != "TRUE" ) { echo "You should not be here"; } else if( $verified = NULL ) { echo "Nulled out"; } $cookie=$HTTP_COOKIE_VARS['auth']; If( $cookie != "Verified_Power") { echo "No Cookie, or not correct cookie"; } // A way to view all cookies //print_r($_COOKIE); ?> <html> <head> <Title>Power's Control Panel </Title> </head> <body> This is my control panel <BR> test<br> </body> </html> Any help would be greatly appreciated! Remember I am new so I am sure my code is poorly written. Please be polite. I am trying to pass a variable as a cookie from one page to another, but can't seem to get it working. The first page has a form with a submit button. The forms action goes to a second page called updated.php. In the updated.php page I have the following code (with the cookie right at the top before anything else): <?php setcookie("get_orderID", $get_orderID, time()+5); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> etc... Later in this page I have the code to send the original form data to an sql database and then retireve an auto increment value back from the database as follows: <?php if($edit_orderID == 0) { $submit_order_sql = "insert into my_orders (date, name, clientID) values ( \"$date\", \"$name\", \"$clientID\" )"; $submit_order_rs = mysql_query( $submit_order_sql, $conn ); $get_order_sql="select orderID from my_orders where job_no = $job_no"; $get_order_rs = mysql_query( $get_order_sql,$conn ); $get_order_row = mysql_fetch_array( $get_order_rs ); $get_orderID = $get_order_row["orderID"]; ?> Finally I need to send the variable I have found back to the form page (using a cookie). But it seems that as the variable is being created at the bottom of the page, the cookie at the top is not seeing the variable. Can anyone help please?? Thanks. I got a weird problem; I got a form whit 2 fields and a submit button; When i click the submit button a jquery ajax script create a cookie named form; My problem is the form isn't disappearing when i click the submit(javascript is set to reload location) button even when i try to delete the cookie from the browser; on index: Code: [Select] function addcomment($pass , $pst)//add comment and call form { $cpageid = $pass; $comment = new Comment; $comment->storeFormValues($pst); $comment->insertc(); require('comment.php'); } on the page where the form is called: Code: [Select] <div id="commentbox" class="container_24"> <?php if(!isset($_POST['form'])) { addcomment($result['article']->id,$_POST);//addcoment on the page whit the current id } ?> </div> Hi, 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 <? $name = $_GET['name']; $game2 = "2": setcookie('game_".$name."',$game2, time()+3600); ?> would this work? if not how can it be done? cheers matt Hello All I wrote a tiny piece of cookie code (to set and record cookies and hits) that works pretty well but seems to have a bug in it that I cannot figure out. First, here is the code: Code: [Select] <?php $my_cookie = (isset($_COOKIE['my_cookie'])) ? true : false; $countFile = "./data/count.txt"; $cookieFile = "./data/cookie.txt"; $dateTime = date('Y-m-d\,H:i:s\,U\,T\,O\G\M\T'); $remoteIPAddress .= $_SERVER['REMOTE_ADDR']; $remoteUserAgent .= $_SERVER['HTTP_USER_AGENT']; $cookieVal = $dateTime; $cookieVal .= ","; $cookieVal .= $remoteIPAddress; $cookieVal .= ","; $cookieVal .= $remoteUserAgent; $cookieVal .= "\r\n"; if(!$my_cookie){ // read the visit counter $countHandle = fopen($countFile, 'r'); $visitCount = intval(trim(@fread($countHandle, filesize($countFile)))); fclose($countHandle); // incremenet the visit counter $visitCount++; // write the new visit count and cookie value to their respective files $countHandle = fopen($countFile, 'w'); $cookieHandle = fopen($cookieFile, "a+b"); if(!preg_match('/bot/',$remoteUserAgent)){ fwrite($countHandle, $visitCount); fwrite($cookieHandle, $cookieVal); } fclose($countHandle); fclose($cookieHandle); setcookie("my_cookie", $cookieVal, time()+31556926); } else{ // read the visit counter $countHandle = fopen($countFile, 'r'); $visitCount = fread($countHandle, filesize($countFile)); fclose($countHandle); } ?> What is happening is that sometimes, not every time, but, sometimes my cookie.txt count gets a 0 (zero) written back to it and I can't figure out why. Can anybody see anything in there that would have this effect? Like I said, it only gets zeroed out from time to time. Thanks to all for taking the time to read. I have an array of fields: $fields = array( "cfg_change_date" => "Configuration Change Date", "system_name" => "System Name", "system_manf" => "System Manufacturer", "system_model_no" => "System Model Number", "system_serial_no" => "System Serial Number", "system_board_part_no" => "System Board Part Number", "system_board_serial_no" => "System Board Serial Number", "asset_tag" => "Asset Tag", "sla_code" => "SLA Code", "category" => "Category", "os_version" => "OS Version", "os_upgraded" => "OS Upgraded", "bios_version" => "BIOS Version", "bios_upgraded" => "BIOS Upgraded", "ilom_version" => "ILOM Version", "ilom_ip" => "ILOM IP Address", "ilom_updated" => "ILOM Updated", "rack_id" => "Rack ID", "rack_location_id" => "Rack Location ID", "tech_id" => "Tech ID", "warranty" => "Warranty" ); I have them all outputted as checkboxes right now in a form. When the user hits save, I want the checked ones to be stored in a single cookie and then later referenced so it remember what the user wants to query from the DB. The thing I cant figure out is how to do this dynamically rather than checking if each one is checked. 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> |