Jump to content

Cookies and PHP.


dh526

Recommended Posts

Right, this is my second message on here because I'm a beginner to php. It is very interesting though and last time people were VERY helpful :)

 

This might be a very basic question but I'm not sure quite where I'm going wrong even though I kind of know it will be something silly I am doing wrong :S

 

This question relates to cookies...

 

I know how to set a cookie, with

setcookie('username', $username)

 

however I do not know how to set $username to be a value that is input by the user into a form. I am creating a login system for a website and this will make everything else fall into place (i hope) so any help would be much appreciated:)

 

 

So i want to do something like this:

 

User enters name into a text field in a form called 'username'

 

the value of 'username' is stored in the cookie.

 

then in another page this can be accessed and printed

 

 

Thank you so so much for your help :)

Link to comment
Share on other sites

firstpage.html

<form action="secondpage.php" method="post">
     <input type="text" name="username">
     <input type="submit" name="submit" value="Login">
<form>

 

secondpage.php

$username = $_POST['username'];

 

Link to comment
Share on other sites

I'm free-handing this so the syntax might be wrong but...

$safe_name = '' ;
if ( isset($_COOKIE['username'] ) 
   $safe_name = htmlentities($_COOKIE['username'], ENT_QUOTES) ;

echo '<input type="text" name="username" value="', $safe_name, '" />';

CR

Link to comment
Share on other sites

Thank you :) Still not 100% done though...

 

I have login.html

 


<html>
<head>
</head>
<body>


Please login
<br>



<form method="post" action="login1.php">

username:<input type="text" name= "username"/>


<br>

<input type="submit" />
</form>






</body>

</html>

 

and login1.php

 

<html>
<head>
</head>

<body>

<?


$username = $_POST ['username'];
setcookie('username', $username);
echo "<a href="login2.php">LINK</a>"; 

?>


</body>
</html>

 

and then i want to show the username in another page, called login2.php

 


<?

echo $_COOKIE['username'];


?>

 

again, I don't know why this isn't working :S

 

Sorry, I feel such a plonker while doing this sometimes ...

 

 

Link to comment
Share on other sites

login1.php

 

<?php
$username = $_POST ['username'];
setcookie('username', $username);
?>
<html>
<head>
</head>
<body>
<?php
echo "<a href="login2.php">LINK</a>"; 
?>
</body>
</html>

 

HTTP headers must be sent BEFORE any HTML (body of the response). And a cookie is a header. I moved your setcookie up in the code. You can also use ob_start output buffering to get around this restriction. http://us3.php.net/manual/en/function.ob-start.php . And you could instead store the value $_SESSION and it would be available in login2.php.

 

CR

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.