PHP - Cookies/session
Ok guys heres the deal. I have been struggling on this for about two weeks and am giving up.
Because I'm giving up, I would like to throw this up to you guys to help me out. I am trying to build a login I tried learning sessions but they make no sense to me whatsoever on how the information passes from one to another. So i created my own sessions database, where im uploading the data i need to pass page to page to my db, and repulling it on the next page. In order to do this i needed to create a cookie so I can refrence that data on the next page, main.php. When I run main.php i get an the echo of. "THIS COOKIE AINT FUCKING WORKING". which to me means one of two things. One is either im not creating the cookie right, or two, im not accessing it correctly. But to widdle that down I created the testpoint on if cookie is created and it seems to work fine, cuase im not getting the die(). Im not an advance php programmer by any means, just been self learning for a few months, so bear with me and please break it down as much as possible. I feel that if i can just get a userid created on one page, put it into a cookie, send it, and retreive it when on the next page, i can do the rest from there. If you can see why that is happening then let me know. Thanks in advance. NEVERBEGOSU ---------------------------------- index.php ------------- Code: [Select] <?php include ('srvdbcon.php'); include ('functions/sitebuild.php'); include ('functions/functions.php'); setcookie('user', 'mike'); login(); headcontent(); headermain(); leftcontent(); ?> <div id="content"> <div class="post"> <h1 class="title"><a href="#">Welcome to </a></h1><p class="byline">21 April 2011 by <a href="#"></a></p> <div class="entry"> <p><strong></strong> is a free, 100% Starcraft 2 Replay Center. Our goal is to provide the ability to UPLOAD replays with detailed data, as well as to DOWNLOAD those replays. Our Replay Search function is one of a kind, designed by hand to help you find replays that you are looking for.</p> <p class="links"><a href="#" class="more">About Us</a></p> </div> </div> <div class="post"> <h2 class="title"><a href="#">Latest News</a></h2> <p class="byline">Working hard to get the site up and running. <a href="#"></a></p> <div class="entry"> <h3>Looking for late May Release:</h3> <blockquote> <p>Im working really hard guys. Forgive me for the pushback.</p> </blockquote> <h3>In Production:</h3> <ul> <li>Working on a new template for the site.</li> <li>Continuing developing the search function for replays.</li> <li>In the middle of moving in RL so working hard when I can.</li> <li>Working hard on the member login/registration pages.</li> <li>Started working on the forums</li> </ul> <h3>Finished:</h3> <ul> <li>Got the Upload Page working.</li> <li>Completed the Download style for the site.</li> <li>Finished a few other smaller aspects.</li> </ul> <p class="links"><a href="#" class="more">Read More</a></p> </div> </div> <div class="post"> <h2 class="title"><a href="#">LOOKING FOR HELP </a></h2> <p class="byline"><small>Posted on 22 Aprl 2011 by <a href="#">SCG.Admin</a></small></p> <div class="entry"> <p> is looking for a few people, if willig, to help design the site. The following positions are needed. </p> <ul> <li>Graphics Designer</li> <li>PHP/MYSQL Programmer</li> </ul> <p>If anyone is intrested please contact me at .</p> <p class="links"><a href="#" class="more">Read More</a></p> </div> </div> </div> <?php rightcontent1(); echo "<center><h2>Login</h2></center> "; echo "<form name='log' method='POST' action='" . $PHP_SELF . "'>"; echo "Username:"; echo "<p><input type='text' name='username'>"; echo "<p>Password:"; echo "<p><input type='password' name='password'>"; echo "<p><input type='submit' id='log' name='log' value='Login'>"; echo "<br>"; echo "<h2> Not a Member </h2>"; echo "<p><a href='register.php'>Register</a></p>"; echo "</center>"; rightcontent2(); footerdiv(); ?> ------------------------------------------------------------------------------------------- main.php --------- Code: [Select] <?php include ('srvdbcon.php'); include ('functions/sitebuild.php'); if(!isset($_COOKIE['userid'])); { die ('CANT GET THE FUKCING COOKIE TO WORK'); } headcontent(); headermain(); leftcontent(); ?> <div id="content"> <div class="post"> <h1 class="title"><a href="#">Welcome to </a></h1><p class="byline">21 April 2011 by <a href="#">SCG.Admin</a></p> <div class="entry"> <p><strong></strong> is a free, 100% Starcraft 2 Replay Center. Our goal is to provide the ability to UPLOAD replays with detailed data, as well as to DOWNLOAD those replays. Our Replay Search function is one of a kind, designed by hand to help you find replays that you are looking for.</p> <p class="links"><a href="#" class="more">About Us</a></p> </div> </div> <div class="post"> <h2 class="title"><a href="#">Latest News</a></h2> <p class="byline">Working hard to get the site up and running. <a href="#">SCG.Admin</a></p> <div class="entry"> <h3>Looking for late May Release:</h3> <blockquote> <p>Im working really hard guys. Forgive me for the pushback.</p> </blockquote> <h3>In Production:</h3> <ul> <li>Working on a new template for the site.</li> <li>Continuing developing the search function for replays.</li> <li>In the middle of moving in RL so working hard when I can.</li> <li>Working hard on the member login/registration pages.</li> <li>Started working on the forums</li> </ul> <h3>Finished:</h3> <ul> <li>Got the Upload Page working.</li> <li>Completed the Download style for the site.</li> <li>Finished a few other smaller aspects.</li> </ul> <p class="links"><a href="#" class="more">Read More</a></p> </div> </div> <div class="post"> <h2 class="title"><a href="#">LOOKING FOR HELP </a></h2> <p class="byline"><small>Posted on 22 Aprl 2011 by <a href="#">SCG.Admin</a></small></p> <div class="entry"> <p>SCG.O is looking for a few people, if willig, to help design the site. The following positions are needed. </p> <ul> <li>Graphics Designer</li> <li>PHP/MYSQL Programmer</li> </ul> <p>If anyone is intrested please contact me at </p> <p class="links"><a href="#" class="more">Read More</a></p> </div> </div> </div> <?php rightcontent1(); rightcontent2(); footerdiv(); ?> ---------------- functions.php Code: [Select] <?php function verifylogin() { } function login() { $Login=$_POST['log']; if ($Login) { $username=$_POST['username']; $password=($_POST['password']); $verlogin="SELECT * FROM user WHERE user='$username' AND password='$password'"; $verloginq=mysql_query($verlogin); if(mysql_num_rows($verloginq)!='0') { setcookie('userid' , 'mike'); if (setcookie) { $ip = getenv("REMOTE_ADDR"); $time=time(); $loginquery="INSERT INTO login (id,user,date,ip) VALUES ('','$username','$time','$ip')"; $loginqueryr=mysql_query($loginquery); if ($loginqueryr) { header("location:testmain.php"); } else { die('There was a problem sending info to database: LOGIN.'); } } else { die ('Cookie wont set for some damn reason.');} } else {die ('username/password error');} } else { } } function logout() { } function logcook() { } function logsess() { } ?> MOD EDIT: [code] . . . [/code] BBCode tags added. Similar TutorialsHello all..... Grasshopper here......... Need help with the following: I have a registration form for a Sport Gym Complex offering a free guest pass if you register...... Where would I implement cookies or sessions to reject 2nd times registrants trying to take advantage of the free guest pass within the year; I have attached the php.file Thanks Steve What is the best practice to use cookies and sessions? Should i create a cookie and keep inside the cookie the name of the username the user has logged in or a session? How am i supposed to compine theese two? Is there any example or a tutorial on this? For example how does php freaks sessions and cookies work. Warning: Cannot modify header information - headers already sent by (output started at /berw/ugrad1/base/s/seh8/public_html/cs25010/index.php:3) in /berw/ugrad1/base/s/seh8/public_html/cs25010/index.php on line 4 Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /berw/ugrad1/base/s/seh8/public_html/cs25010/index.php:3) in /berw/ugrad1/base/s/seh8/public_html/cs25010/index.php on line 7 Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /berw/ugrad1/base/s/seh8/public_html/cs25010/index.php:3) in /berw/ugrad1/base/s/seh8/public_html/cs25010/index.php on line 7 here is the start of the code, I have do idea what is happening, help please! <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <?PHP setcookie('user', 'sianiuser'); ?> <?php session_start(); if (isset($_SESSION['sianiuser'])) { unset ($_SESSION['sianiuser']); } ?> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/> Hello, I have the following code. I am trying to have it same the searches save for 30 days. I can't tell exactly how long this works for because it does work for a while but when I open the browser the following day, the information is lost. The server time is accurate. Does anyone have any clue to why this might not work? session_set_cookie_params(2592000); session_name('test_mysearches'); session_start(); $rqstsignature = md5($_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING'].print_r($_POST, true));if(!isset($_SESSION['mysearches'])) { $_SESSION['mysearches'] = array($_GET['s']);} else { if ($_GET['s'] != '') { $_SESSION['mysearches'] = array_filter($_SESSION['mysearches'], 'strlen'); if ($_SESSION['LastRequest'] != $rqstsignature) { // not a refresh array_unshift($_SESSION['mysearches'], $_GET['s']); $_SESSION['LastRequest'] = $rqstsignature; while(count($_SESSION['mysearches']) > 5) { array_pop($_SESSION['mysearches']); } } } } () thanks in advance. I'm pulling my hair out here... Here's my problem. I'm about halfway done with my site (It's a site to sell stuff and I'm working on the shopping cart) when I noticed a big problem. I've got session cookies working in every browser (FF, chrome, safari) except Internet Explorer. For some reason IE likes to delete my session cookies so there's nothing there after I click 'view shopping cart'. I've verified that the session is set but then it gets deleted. Now I am using IFrames and I searched for possible causes and tried changing headers, making sure there's no underscores, etc... No joy The problem is that getting the problem to show up would require a lot of copying code onto this page since it's like 4 or 5 pages to get something into the cart and then view the cart itself. What do you folks recommend? Should I just link to the site itself? What's the best way to store shopping cart information? I want things to stay in the cart for up to 2 weeks after they're added, without the need for somebody to log-in to their account to store them. Should I store the information in a database some how and then set a cookie referencing the cart_id in the db? Hi All, first of all. Sorry for signing up to the site and then immediately asking for help. I hate doing it but always seems to be the way people get involved with forums these days and it seems I am no different... So thank you in advance if you are kind enough to answer! Among many personal flaws, my issue is a technical one regarding the ability to retain session variables. Unfortunately whilst the website itself is not complicated, the problem is not as simple as just running session_start(); and letting it get on and do its own thing. So the problem exists around a relatively simple contact form and the basic Turing test that it uses to prove the user is human. The script adds a value to the $_SESSION variable and then on the next page load, tests to see if the variable is still there. As per this little function: function cookiesEnabled() { if ($_SESSION['cookiesEnabledTest']) $_SESSION['cookiesEnabled'] = true; else $_SESSION['cookiesEnabledTest'] = true; } Now for what must be 95% of all users, the variable is still there, proving the the session is being maintained as the user navigates the site. Unfortunately a small group of computers appear to be having difficulties with this for a not too clear reason. The computers have varying browser user agents indicating that they are all (so far) using one of the following: MSIE 8 MSIE 7 MSIE 7 in what appears to be MSIE 6 compatibility mode Now in testing, I can't get MSIE 8 or MSIE 7 to produce the same effect, with exception to MSIE 8 in "InPrivate" browsing mode. In this mode it is easy to see that there is no cookie txt file being created and thus no way of the browser to pass back the session id on the next page load. I can't think of any detail that I have missed out, but if there is anything I have left unclear, please let me know! Thanks again! Felix I am trying to create an index page which contains registration and login field the problem that i get is on successful login a warning is displayed session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\Eventz.com\index.php:116) in C:\xampp\htdocs\Eventz.com\index.php on line 235 This is the login part of my index.php this tag is inside an html table below the login form I also have a registration form and its php code above the login form Code: [Select] <?php if (isset($_REQUEST['pass'])) { $id=$_POST['id']; $pass=$_POST['pass']; $conn =mysql_connect("localhost","root",""); if (!$conn) { die('Could not connect: ' . mysql_error()); } /* checking connection....success! */ $e=mysql_select_db('test', $conn); if(!$e) { die(''.mysql_error()); } else { echo 'database selected successfully'; } if (isset($_REQUEST['id']) || (isset($_REQUEST['pass']))) { if($_REQUEST['id'] == "" || $_REQUEST['pass']=="") { echo "login fields cannot be empty"; } else { $sql=mysql_query("Select email,password from login where email='$id' AND password='$pass'"); $count=mysql_num_rows($sql); if($count==1) /* $count checks if username and password are in same row */ { session_start(); $_SESSION['id']=$id; echo "</br>Login Successful</br>"; } else { echo "</br>invalid</br>"; echo "please try to login again</br>"; } } } } ?> Any help or suggestion would be appreciated I am having trouble resolving an error. Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/s519970/public_html/header.php:27) in /home/s519970/public_html/admin/login.php on line 2 What I can gather is I can't use "header (Location: 'admin.php')" after i've used session_start(). I have tried to replace the header (Location: 'admin.php') with this: echo "<script>document.location.href='admin.php'</script>"; echo "<script>'Content-type: application/octet-stream'</script>"; I've been trying to read up on solutions but haven't been able to get it sorted. If anyone can offer some advice that would be greatly appreciated as im new to php. Code: [Select] <?php session_start(); if(isset($_SESSION['user'])) echo "<script>document.location.href='admin.php'</script>"; echo "<script>'Content-type: application/octet-stream'</script>"; ?> <div id="loginform"> <form action="dologin.php" method="post"> <table> <tr> <td><span>Username:</span></td> <td><input type="text" name="username" /></td> </tr> <tr> <td><span>Password:</span></td> <td><input type="password" name="password" /></td> </tr> <tr> <td colspan="2" align="right"><input type="submit" name="login" value="Login" /></td> </tr> </table> </form> </div> I have tried using require_once('yourpage.php'); before my <head></head> tags in the header document where I've specified the html information but this doesn't seem to work. I've been advised to use ob_start("ob_gzhandler"); but I am not sure how to implement this. Any advice is greatly appreciated! in this page http://maximaart.com/newscp/ i have this problem Code: [Select] Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/maximasy/public_html/newscp/index.php:1) in /home/maximasy/public_html/newscp/index.php on line 2 my source code is <?php session_start(); include_once("config.php"); include_once("functions.php"); $errorMessage = ''; if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) { if ($_POST['txtUserId'] === "$user" && $_POST['txtPassword'] === "$pass") { // the user id and password match, $_SESSION['basic_is_logged_in'] = true; require("main.php"); exit;?> I'm making a simple login system with MySQL and PHP (very simple, I'm just starting with PHP). The MySQL portion is done, but I need to ensure only people who are logged in can see certain content. To check if people are logged in, my website checks that they have the $_SESSION['user'] variable set. If it is set, then it lets them continue through the website, if not, it tells them to login. Is that enough security, or can people simply inject a session cookie into their browser to spoof that they are logged in? My idea was to generate a session key cookie when they login (just a random string of letters and numbers) and store that in the database, then on every page, check to make sure their session key is the same thing that's in the database. Is this necessary? It seems expensive. hi everyone. i'm wondering what the best way is to create a session variable and pass it to an iframe. i need to do something along these lines, but it doesn't seem to pass the ID. Any hints on how i should accomplish this? Code: [Select] session_start(); $_SESSION['ID']=$_GET['ID']; // id from previous page $ID=session_id(); <iframe src="iframepage.php?ID=<?php echo $ID; ?>" style="width:680px; height:200px;" noresize="noresize" frameborder="0" border="0" scrolling="Yes" allowtransparency="true" /> </iframe> Evening! I've been iffing and ahhing over this and well im not too sure, hence the post. Code: [Select] // Redirects if there is no session id selected and echos the error on the previous page if(!isset($_GET['get']) || ($_GET['getget'])){ header("Location: #.php?error"); } So it should simply check if get is set if it isnt then see if getget is set? If not redirect and show the error. Now ive tried it and even when get/getget is set it still redirects, probably something silly. Care to share anyone? Harry. Just curious how other people feel about this. I am working on an application where a lot of info is pulled from MySQL and needed on multiple pages.
Would it make more sense to...
1. Pull all data ONCE and store it in SESSION variables to use on other pages
2. Pull the data from the database on each new page that needs it
I assume the preferred method is #1, but maybe there is some downside to using SESSION variables "too much"?
Side question that's kind of related: As far as URLs, is it preferable to have data stored in them (i.e. domain.com/somepage.php?somedata=something&otherdata=thisdata) or use SESSION variables to store that data so the URLs can stay general/clean (i.e. domain.com/somepage.php)?
Both are probably loaded questions but any possible insight would be appreciated.
Thanks!
Greg
Edited by galvin, 04 November 2014 - 10:30 AM. When a user logs in it sets a cookie with their user id and sets the time they choose(either a session cookie or a cookie lasting one year for users who wish to stay logged in). when they select to stay logged in forever and close the browser the next time they open it, it tells them they arent logged in. however when they go to a new page they appear to be logged in. What i dont understand is why they have to go to a new page for it to say they are logged in. Here is the code which runs everytime the site is load if (isset($_COOKIE['uid'])) { $user->setup($_COOKIE['uid']); } user setup basically selects their info from the database and sets their username and other info to variables. Anyone know a better way to do this? Hey all, http://www.adamrowden.co.uk/photo-searchr.asp If you visit this link, you'll see on the left side you have a few photos. If you click a photo from that page and then go back to the link i've provided you'll see that it shows you your 'recently viewed photos'. I'm wanting to do exactly that and need some pointers on how i would!? I'd be very grateful for any help Thanks, Jimmy m Hello all, I hope someone can shed some light/point me in the right direction. I have a site that allows you to search for a customer then view and change their detail. page 1 has a search box for name entry. page 2 displays all the matches retrieved from a db table that match. The user selects which customer is the correct 1 and sets a cookie containing the selected customers unique id. page 3 allows changing of the customers details. now the problem I have: If a user navigates to page 3 and has a customers details on page for viewing all is well. If they open a new tab (leaving page 3 open on first tab) and go to page 1, search for another customer, page 2 select another customer (which overwrites cookie) then to page 3. They now have two tabs open on page 3 both displaying different customer details. If they return to the first tab and change some detail, when they save it actually updates the users details that corrispond to the second tab. I know this is because the cookie has been changed that holds the unique id that is required for the update query. How can I prevent this? I've looked at sessions but it would seem the same issue would excist. Am I wrong? Many Thanks I hope I made sense. This is prob a stupid question.. but i've always wondered..
When deleting a cookie why do we use
why do we use:
setcookie($name, '', time() - 3600, "/", "", 0);
when this works just fine:
setcookie($name, '', 0, "/", "", 0);
isn't the time() just a waste of space?
I ask this because everywhere i look i see:
setcookie($name, '', time() -3600 , "/", "", 0);
I have this page where I create two cookies with the username and passwords of the users for the website. Code: [Select] $equipa = $_POST['equipa'] ; $pass = $_POST['codigo'] ; setcookie("equipa", "$equipa", time() + 3600) ; setcookie("codigo", "$pass", time() + 3600) ; In the next page, I can have access to the cookie, with this script : Code: [Select] <?php $equipa = $_COOKIE['equipa'] ; echo $equipa ; $pass = $_COOKIE['codigo'] ; echo $pass ; ?> But in this page you have to fill a form and send it, going to a new page. In this new one, with the exact same code, I can't access the cookie. If anyone could please help me and tell me why..... Thanks for the help hello; it seems strange that php can set cookies, since php is on the serer-side, but the cookies are on the client-side ... is there something that I am missing? |