Create a cookie with PHP - php

I don't understand where is my mistake:
Page a.php:
setcookie("user",$username,time()+300);
Page b.php:
echo $_COOKIE['user']; or echo $_COOKIE["user"];
The result is that echo doesn't print nothing!
Thank you very much for the help!

Either $username has no value or you have white-space before you are setting the cookie. Cookies must be sent with the header information before any HTML is sent at all so make sure the

Related

How can i get the session start username to echo out

Iam having issues echoing the session username and have no idea were to start any more cause every thing i have tried keeps saying string to array conversion but idk how to get this to correctly work. here is the code that would check for the loged in saying you logged in would you like to log out.
if(isset($_SESSION['username'])&& !empty($_SESSION['username'])){
echo "<P id='loged'>your loged in would you like to log out</p>";
echo'log out';
}else{
include 'logform.php';
}
You probably want to call session_start() before the $_SESSION variable will be available.
You just want to echo the username?
echo "Hello " . $_SESSION['username];
I suppose the following answers your question...
echo $_SESSION['username'];
...but you will also want to use session_start() first.
Also, no need to use isset() and !empty(), as empty() checks if it is set first. All you really need is:
if(!empty($_SESSION['username']))
You forgot to mention session_start() in the begining.
session_start(); //add this in the begining of the file.
echo $_SESSION['username']; //this will display username
Try this:
call the function session_start() at beginning of the page and then check the $_SESSION array using
echo '<pre>';
print_r($_SESSION);
after that you see all the indexes set in session variable.
Thanks

php sessions not working correctly

Hello i am having problems holding sessions from page to page, code worked on my previous servers running php5 but not on my recent server, i am wondering whether its a bug?
<?php
session_start();
$_SESSION['session'] = $_POST['session'];
header("location: www.mysite.com/page1.php");
?>
<?php
session_start();
echo "Good morning" . $_SESSION['session']; //returns empty session always.
?>
ANy ideas? session is held on first page but not on the second.
In case you missed it, make sure you do a session_start() at every page you're using the $_SESSION variable.
You should check your php.ini file and see what's going on.
Make sure session.use_cookies = 1 and session.save_handler = files.
Use this test page to see whether it's a general PHP problem or just your code.
<?php
session_start();
if(isset($_SESSION)){
echo "Session variable exists<br/>";
if(!isset($_SESSION['test'])){
$_SESSION['test'] = "Success!";
echo "Variable has been set, refresh the page and see if stored it properly.";
}else{
echo $_SESSION['test'];
}
}else{
echo "No session variable has been created.";
}
?>
If that worked, then it's got to do with your code.
If you're setting your session variable to $_POST['session'] am I to assume you submitted a form with an input with the name session?
This setup should work.
index.php
<form action='page0.php' method='POST'>
<input type='hidden' name='session' value='SPAAAAACE' />
<input type='submit' />
</form>
Page0.php
<?php
session_start();
$_SESSION['session'] = $_POST['session'];
header("location: www.mysite.com/page1.php");
?>
Page1.php
<?php
session_start();
echo "Good morning" . $_SESSION['session'];
?>
For completeness and debugging purposes
In case you are using cookie-less sessions, you have to manually add the SID (session id) to the header redirect like this
header("location: www.mysite.com/page.php?".htmlspecialchars(SID));
If the problem still persists, it could be a permission issue.
Maybe you're not allowed to read the session file stored on the server?
Update: OP commented that it was a permission issue and the problem is now resolved
Turn on error reporting temperately with:
error_reporting(E_ALL) This may spit out an error related to your problem. Most likely an undefined index session notice.
You should always have a check in place on Super Globals.
<?php
session_start();
$_SESSION['session'] = (isset($_POST['session']))?$_POST['session']:null;
header("Location: www.mysite.com/page1.php");
die;
?>
Your code seems correct though I'm pretty sure $_POST['session'] is empty.
You should try this :
<?php
session_start();
$_SESSION['session'] = 'John Doe';
header("location: www.mysite.com/page1.php");
?>
<?php
session_start();
echo "Good morning" . $_SESSION['session']; //returns empty session always.
?>
To see if this works or not. I guess it will.
IF not, take a look at your cookies, maybe they are disabled.
Then, if it works, I probably because $_POST['session'] is null or empty, are you sure you posted something like <input type="text" name="session" /> ?
You need to pass the session id with the redirect.
Also make sure you use session_start() at the top of EVERY page that needs a session
First try using
<?php session_start();
instead of
<?php
session_start();
If the problem still exists, then open your script in Netbeans editor and see whether any unexpected characters found at very beginning of the the script.
In addition, please make sure that $_POST['session'] has a value to assign in $_SESSION['session'].
You will have to call
session_start();
on the first line of every page you want to retain the session in

Session ID and variables in PHP

I have a login page where once a user is logged in successfully it echos a link to their personal page. When that page loads I want it to check if the user has access to it so someone doesn't try to just type in www.mywebsite.com/bob.php in the url. I tried to use a cookie to send the user info but I realized you can't use cookies after html has been written to the page. Does anyone know an efficient way to do this that is also fairly simple? Thanks
After the user logs in, assign his id to a session variable:
<?php
session_start();
$_SESSION["userid"] = $userid;
?>
On the protected page, check if the user has a $_SESSION["userid"] variable set:
<?php
session_start();
if (isset($_SESSION["userid"])) {
//show page
}else{
echo "No rights";
}
?>
It is true that you cannot set cookies when output has already been sent to the browser. A useful trick is to use output buffering. Basically, you begin your code with a call to ob_start() and end it with ob_end_flush(). Now you can set cookies (and any HTML header) wherever you want in your code.

$_SESSION values not holding!

I'm writing a user login system, and I (like so many others) am having a problem with my sessions.
Here's the pointer from the login script when the inputs are validated:
session_start();
$_SESSION['id']=$id;
header('location: memberhome.php');
Here's the first thing on memberhome.php:
<?php
session_start();
$id=$_SESSION['id'];
?>
And later in memberhome.php:
You are logged in as: <?php echo $id; ?>
The problem is $_SESSION['id'] is apparently empty so the echo $id prints nothing.
An alternate that also does NOT work:
//removed session_start and $_SESSION bit from the top
You are logged in as: <?php session_start(); echo $_SESSION['id']; ?>
NOW, here's the weird part. This method DOES work:
You are logged in as: <?php echo session_start();$_SESSION['id']; ?>
You can see the session_start() is moved AFTER the echo. This works when the page loads from the login script. However, upon refresh, it does NOT work once again.
I've tried a bunch of alternatives and spent a few hours searching for answers in previous questions. I also looked at my phpinfo() for something fishy and found nothing. This is entirely what my progress is hinging on. Thanks!
First of all, please enable debugging:
error_reporting(E_ALL);
ini_set('display_errors', '1');
Second, session_start() needs to be at the top of the page. So the line you wrote;
You are logged in as: <?php echo session_start();$_SESSION['id']; ?>
will never work.
The following line needs to be on top of the page, before any HTML etc.
<?php
session_start();
$id=$_SESSION['id'];
?>
Have you tried:
print_r($_SESSION);
to examine the contents of the session?
Make sure you're calling session_start() before you output anything on the page. The standard cookie-based sessions require some header information to be exchanged, which must be done before you send any content.
You're most likely running into output buffering, which is why it sometimes works and other times it does not. Generally speaking, stick to starting the session before any output is generated, you'll find your code works better.
use
ob_start(); #session_start();
on the top of the both page

page not redirecting

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.

Categories