PHP Help

Hi there :smiley:

I think it’s a stupid question, but I can’t find the solution for this thing I would do :frowning:

I created a page, that here I call it “sl.php”:

<?php echo "<a href=\"www.domain.com/index.php?name=test\">Click here!</a>"; ?>

Clicking on that link, it report to the “index.php” page where I wrote this code:

<?php echo "My name is " . $_REQUEST['name'] . "<br />"; ?>

Now there is my problem. I want that the word requested by using $_REQUEST[‘name’] (in this case it’s “test”) will be available also in other pages, such as “faq.php”. So I put, always in the “index.php” page, that code, but I think it’s wrong:

<? session_start(); echo "My name is " . $_REQUEST['name'] . "<br />"; setcookie('name', 'name', time()+60*60); $_REQUEST['name'] = $_COOKIE['name']; ?>

… and in the “faq.php” page:

<?php echo "Hello " . $_COOKIE['name'] . "!!!<br />"; ?>

What I wrong? I’m sorry, I’m a newbie with php. Please be patience ;D

Thank you very much in advance for your help.

Andrea

no answer? :frowning:

Hi. For your index.php page the correct code would be like this:

[php]<?
if($_REQUEST[‘name’]!=’’){
setcookie(‘name’, $_REQUEST[‘name’], time()+60*60);
$_COOKIE[‘name’]=$_REQUEST[‘name’];
}
echo "My name is " . $_COOKIE[‘name’] . “
”;
?>[/php]

And your code for other pages is fine.
Just one note. You do not need to call session_start() if you’re saving variable to cookie. You need this only if you save something to session variable ($_SESSION array). The difference is that $_SESSION variable will live until you close browser window.

Thank you sooo much! :smiley: It works.

Can I ask you another(last) thing, please? Now that it works, I need to create a form where users have to write their names and email addresses, and these data are sent to me by email. I little know how to do this, but how can I include in the email with all the data also the cookie? I mean that in the email it should also appear the word “test”.

I created a page “contact.htm”:

[code]
Name:

E-mail:

Completed offer:

Message:

[/code]

… and the page “send_contact.php”:

[code]<?php
$indirizzo="[email protected]";
{
mail($indirizzo,“Message from $nome: $oggetto” ,"$nome - $email has sent the following message:" ,"$messaggio");

echo (“The message has been sent. Thank you.”);

}
?>
[/code]

I would also add a “referer” field where appear the word of the cookie.

Thank you very much in advance for your patience and help :slight_smile:

Andrea

Andrea, you just need to read that cookie from the send_contact.php script:

[php]<?php
$indirizzo="[email protected]";
$referer=$_COOKIE[‘name’];

// append referer to message
$messaggio.="\n\nReferer: ".$referer;

mail($indirizzo,“Message from $nome: $oggetto” ,"$nome - $email has sent the following message:" ,"$messaggio");

echo (“The message has been sent. Thank you.”);
?>[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service