I am trying to set a cookie based on the returned value from an insert into mySQL. I know the insert has worked as I have a value for mysqli_insert_id($link). However, he cookie is the same before and after I attempt to run setcookie. Can anyone help? The code I am using is
echo mysqli_insert_id($link)."<br>";
print_r($_COOKIE);
echo "<br>";
setcookie("id", mysqli_insert_id($link), time() + 60*60*24);
print_r($_COOKIE);
echo "<br>";
setcookie() needs to be sent before anything is echoed out to the body of the page. For example:
echo 'test'; //At this point, headers are done, and the body has started
setcookie(...); //Fails
An alternative solution would be to use ob_start() to buffer the output into memory before outputting to the browser. You can do something like this:
ob_start();
echo 'test'; //Output is captured, and stored in memory
setcookie(...); //Nothing has been output yet, so header is set properly
ob_end_flush(); //We're done storing stuff in the buffer, output it to the browser.
Related
I narrowed the problem down to this statement.
if (!setcookie("cookielogin",$usernametocheck, time()+3600)) echo "cookie setup failed<br/>";
Every time I run the code it shows "cookie setup failed" on browser.
I checked the browser for cookies stored by the site and I don't see my cookie.
Can anybody help ?
add ob_start(); at the top of your page and ob_clean() after setting cookie .
<?php
ob_start();
#Code..........
#Code...........
#Code............
if (!setcookie("cookielogin",$usernametocheck, time()+3600)) echo "cookie setup failed<br/>";
ob_clean();
#code......
?>
i have the following code:
if (!isset($_SESSION)) {
ob_start();
}
$_SESSION['views'] = 1; // store session data
echo $_SESSION['views']; //retrieve data
i tried to break it up in 2 parts like this:
//Page 1
if (!isset($_SESSION)) {
ob_start();
}
$_SESSION['views'] = 1; // store session data
.
//page 2
echo $_SESSION['views']; //retrieve data
it echos nothing, what am I doing wrong?
as Gumbo mentioned, you need to call session_start() on every page you want to use the session..
You also mentioned you were getting the error:
Warning: session_start(): Cannot send session cache limiter - headers already sent
This is due to data being output on the page before the session_start() is being called, you have to include this before anything is echo'd to the browser.
Make sure to call session_start on every page you want the session to be available. ob_start is not the session handler but the output buffer handler.
session_start() in 2 files before any output.
For some reason my post id session is not carrying over to my grading script page. I grab the post_id from the posts url in my posts page and turn it into a $_SESSION to carry it to my grading script page. My $_SESSION that holds the logged in users id $_SESSION['user_id'] if logged in carries over to the grading script but not the post $_SESSION. How can I have my posts id carry over to my grading script page?
I have session_start(); at the top of both of my pages.
$_SESSION['post_id'] = $_GET['pid'];
You may have some output before you started the session. If that is the case, the session might not be able to be set correctly. Enable debugging mode and search for an error regarding the session_start() function:
ini_set('display_errors', 'on');
error_reporting(E_ALL);
Or check the error_log if you can't switch debugging on.
The error might look like this:
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\phptests\test.php:1) in C:\xampp\htdocs\phptests\test.php on line 4
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\phptests\test.php:1) in C:\xampp\htdocs\phptests\test.php on line 4
The most common reason for having problems with php sessions is that the php script is outputting something before the session is started.
Ok, you might not have any echo statements, or you might not even have ANY php code apart from reading the session ... but take time to check what happens BEFORE your code even begins.
For example, let's play spot the difference: (only one code will work)
<?php
session_start();
$_SESSION["test"] = "hello";
if($_SESSION["test"] == "hello";
{
echo "Session is working!";
}
else
{
echo "Session is NOT working!";
}
?>
and...
<?php
session_start();
$_SESSION["test"] = "hello";
if($_SESSION["test"] == "hello";
{
echo "Session is working!";
}
else
{
echo "Session is NOT working!";
}
?>
The difference is that there is a single space before the opening ANY data before session_start() will stop the script from working.
I've been there - having an accidental space or blank line at the top of the document will create serious issues, but enabling the php error outputs should show you where you are going wrong if this is the case.
session_start();
$_SESSION['fname']=$row['fname'];
$_SESSION['user']=$row['name'];
$_SESSION['adpoint'] = $row['adpoint'];
$_SESSION['phone']=$row['phone'];
$_SESSION['id'] = $row['id'];
$_SESSION['rememberMe'] = $_POST['rememberMe'];
// Store some data in the session
setcookie('smsapp',$_POST['rememberMe']);
echo "your name"." ".$_SESSION['user'];
Print_r ($_SESSION);
header("Location: http://www.niktrixhosting.com/login/user/index.php");
here in this code header("Location: http://www.niktrixhosting.com/login/user/index.php");
this line is not redirecting it
plz mentionif any another function for redirecting page .
You can't echo and print_r or give any other output before function header.
You're trying to set the headers after sending them. Comment out the echo "your name"." ".$_SESSION['user']; line and everything will work.
Explanation: The headers are always the first thing sent (that's why they're called headers). When you call echo, part of the page is sent, but if the headers aren't set, they will be sent first. What's happening in your code is that the echo line is sending the headers, and then you're trying to set them after the headers have already been sent (which doesn't work).
Headers should be sent before any output, so any statement with an echo or a print_r before a header will cause errors.
try
echo "<script>window.location=\"http://www.niktrixhosting.com/login/user/index.php\"</script>";
instead of:
header("Location: http://www.niktrixhosting.com/login/user/index.php");
you are not to echo/print out/output anything before using header(). that will really affect your PHP page.
I have a php upload handler in upload.php, and there, I have to following
<? setcookie("test",100,time()+3600); ?>
but, when I check the cookies that are set, I dont see any "test" cookie at all.
Can you please help me set a cookie on file upload? why is this upload script any different from any normal script that is accessed by the browser?
Here is the code I have
<?php
if (!empty($_FILES)) {
if(move_uploaded_file($tempFile,$targetFile))
{
setcookie("targetPath",$targetPath,time() + 3600,'/');
print $_COOKIE['targetPath']; // prints fine here
echo 1;
}
else
echo -1;}
else
{
//print_r($_COOKIE);
print "start cookie >> ";
print $_COOKIE['targetPath']; // does not print when I call upload.php standalone
print " << end cookie";
}
?>
This may or may not solve your problem, but I thought I should point it out:
setcookie needs to be called prior to any output, unless you are using output buffering.
The first argument to move_uploaded_file should be something like $_FILES["pictures"]["tmp_name"][0]
Cookies set with setcookie don't show up until the next page load. And yes, this is documented in the PHP manual:
Once the cookies have been set, they
can be accessed on the next page load
with the $_COOKIE or $HTTP_COOKIE_VARS
arrays.
This means that this code:
setcookie("targetPath",$targetPath,time() + 3600,'/');
print $_COOKIE['targetPath']; // prints fine here
should print the cookie's old value.
setcookie returns false if it fails. You might want to check that return value.
Are you checking to see if cookies are set in upload.php, ie. the same script you set them? If so, I wouldn't expect them to be set. The cookie will be sent by the client on the next HTTP request after they have received the cookie from upload.php.
setcookie has "path" argument. If it is not specified: "The default value is the current directory that the cookie is being set in."
So most likely you're trying to set cookie for something like www.youdomain.com/actions/upload.php and in that case cookie will be set for /actions/ path.
Also check that call setcookie is done before any output from your script
Try specifying the domain?
<?php
setcookie( 'test', 100, time()+3600, '/', '.sitename.com' );
Are you fetching it with $_COOKIE['test'] ?
PS - You should not be using short tags. Replace <? with <?php.