Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cookies or/and sessions 1

Status
Not open for further replies.

alexxxis

Programmer
Sep 21, 2006
38
RO
hi. i`m trying to build a website, with a user/password mysql data base. i have to mention i`m new to php.
so , i did the register form [it`s ok], the login form [ok too, selects in the data base, checking ,redirect to the new page if ok]. What i`m trying to do is show a text something like "Hello user" where user SHOULD BE kept in a cookie or something. but it`s not. Ive been burning my brains for half an hour now and i can`t figure out what`s wrong. Here`s what i tried.

i did a html form, and a validation.php file
there i have

<?php
setcookie(user, $_POST[User], time()+10000);
?>
<html>
<body>
<?php
echo $_COOKIE[user];
....
....
HERE it shows me the cookie, but when i try to redirect to the user page, where i have the SAME echo $_COOKIE[user]; ... it nologer prints the cookie on the screen. what am i missing ? this is my last hope :p. thanks
 
remember that a cookie gets sent to a browser with the page download. so the server won't know that the browser has a cookie until the next page refresh.

try your code like this
Code:
<?php
setcookie("user", $_POST['User'], time()+10000);
?>
<html>
<body>
<?php
if (isset($_COOKIE['user'])){
  echo $_COOKIE[user];
} else {
  echo "<a href=\"{$_SERVER['PHP_SELF']}\">Click to refresh</a>";
}
?>
 
I still don`t get it...
i have a form. than i have this file

<?php
setcookie( 'User', $_POST['User'] , time()+10000);
$user = $_POST['User'];
$password = $_POST['Parola'];
if (isset($_COOKIE['User'])){
echo $_COOKIE[User];
}
else {
echo "<a href=\"{$_SERVER['PHP_SELF']}\">Click to refresh</a>";
}
[...database validation for user and password...]
?>

So far things are good. it prints me the user. after i make the database validation, i have a redirect to this file :

<?php
echo $_COOKIE[User];
?>

WHY doesn`t it print it here too ? HELP !
 
1. please put quotes around array elements.

2. cookies are stored on the browser, and sent by the browser when the user requests a resource within the relevant domain (usually).
the $_COOKIES superglobal is populated from the cookies that the browser sends.
so in your code, setting a cookie on the server does not mean that the $_COOKIES superblobal is magically filled. it just means that php knows that when it sends data to the browser it must include a header that contains the cookie information.
the next time the user requests a resource from the domain the browser knows that it needs to send a cookie. the webserver then receives the cookie and knows to pass it to php. php knows what to do with cookies and bungs the data in the $_COOKIES superglobal.
so in short- you cannot access a cookie from the same instance of the script in which it is set. the cookie, at that time, does not exist.

3. your post header refers to sessions too, but you are asking only about cookies. there is a session based solution to your issue if you are interested.
 
here is a session based variant.

Code:
<?
session_name("tek-tips");
session_start();
if (isset($_POST['user'])){
 $_SESSION['myName']=trim($_POST['user']);
} else {
 $_SESSION['user'] = "Anonymous";
}
echo <<<EOL
Hello.  Your name is {$_SESSION['user']}.<br/>

Click <a href="{$_SERVER['PHP_SELF']}">here</a> to prove to yourself that your name is now stored in the server until you close your browser down and restart it.
EOL;
?>
 
i did it 2 hours ago, almost the same [except i didn`t name the session] .. and it didn`t work. it does work now though. Could you please explain what do echo <<<EOL and EOL; do in this script ?
 
naming the session is not important. i do it because i run multiple development projects on my dev machine (which, for simplicity, I keep as localhost) and i want to distinguish between sessions in each app.

this line
Code:
$_SESSION['myName']=trim($_POST['user']);
should, of course, be
Code:
$_SESSION['user']=trim($_POST['user']);

the <<<EOL EOL; syntax is called the heredoc syntax. it's useful for outputting blocks of html without worrying about proper escaping of quotation marks. here is a reference to it:
 
Either i`m drunk, either someone`s messin` with me :p
so. i did what you told me to, and it worked. than i changed something ... and it worked no more. So, again, i have this file. and it echo`s the $_SESSION variables corectly. further more, i have an extra test.php, which echos the same variables corectly.
file 1.
<?php
session_name("tek-tips");
session_start();
$_SESSION['use'] = $_POST['User'];
$_SESSION['pass'] = $_POST['Parola'];
echo $_SESSION['use'];
echo $_SESSION['pass'];
?>
file 2.
<?php
session_name("tek-tips");
session_start();
echo 'welcome' . ' ' . $_SESSION['use'];
echo $_SESSION['pass'];
?>


BUT! i put a redirecting header("Location: i`m testing upon/file2.php"); , so i`m sending him towards file 2., FILE 2. WORKS no more ! this is becoming enoying ... why can`t i make a redirect towards another file, keeping the session variable ? that`s what i want to do .. and i can`t. Thanks jpadie for your help :(
 
you can. but you're probably working on a local machine and you get a race condition on the file that is holding the session data. i.e. the file1.php has not finished writing the data to the session file before file2.php has requested the session file.

so....

add this to file1.php immediately before the redirect
Code:
session_write_close();

this forces php to write all the session data to the file, and save the file (close the session) before going on to the next line. that should work.
 
i must be hopeless ...

<?
session_name("tek-tips");
session_start();
if (isset($_POST['User'])){
$_SESSION['use']=trim($_POST['User']);
} else {
$_SESSION['use'] = "Anonymous";
}
session_write_close();
echo $_SESSION['use'];
?>


<?php
session_name("tek-tips");
session_start();
echo 'welcome' . ' ' . $_SESSION['use'];
?>


those 2 file WORK

file 1, with header("Location: at the end makes file 2 echo ... just welcome. and normally it would have echoed welcome user. one last try and i`m giving up ... either it`s the header() or the server i`m using .. i have no ideea
 
that is strange. you may be able to work around this (in this case) by adapting your header command as follows:

Code:
header("Location: [URL unfurl="true"]http://alexxxis.lx.ro/h2.php?".SID);[/URL]

bear in mind that the first page must have been accessed by the same domain for the cookie based solution to work. so if page1.php was accessed, for example, via then the browser will not know that the session cookie it has received should be sent to alexxxis.lx.ro. if this is the case, and you need it to work this way, you will need to use a different method of identification management (such as the transid method i showed above).

there is a good section in the php manual about sessions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top