Forum Moderators: coopster
Basically i'm having trouble figuring out what else needs to be added to the code for the cookie to work. I'm also confused about what the syntax needs to be after these things below:
$fname =
$lname =
$phone =
$email =
<?phpfunction file_download_help($section) {
switch ($section) {
case 'admin/modules#description':
// This description is shown in the listing at admin/modules.
return t('The file_download module allows SCC to gather information of clients and sends them the link for the requested document.');
break;
case 'file_download/request/fileid':
$output = '<p>'.t('The file you have requested will be sent to the e-mail address provided below.');
return $output;
break;
}
}
function file_download_perm() {
return array('file download');
}
function file_download_menu($may_cache) {
$items = array();
$access = user_access('file download');
if ($may_cache) {
$items[] = array('path' => 'file_download/request', 'title' => t('File Download'),
'type' => MENU_CALLBACK, 'callback' => file_download_request, 'access' => $access);
$items[] = array('path' => 'file_download/sent', 'title' => t('Thank you'),
'type' => MENU_CALLBACK, 'callback' => file_download_sent, 'access' => $access);
// Error Page
$items[] = array('path' => 'file_download/failed', 'title' => t('Error Processing Request'),
'type' => MENU_CALLBACK, 'callback' => file_download_failed, 'access' => $access);
}
return $items;
}
function file_download_request_form($fileid) {
$myCookie = $_COOKIE["mikeknowsall"];
if($myCookie.size > 0)
{
$fname =
$lname =
$phone =
$email =
file_download_request_form_submit2($file_id, $fname, $lname, $phone, $email)
return;
}
$form = array();
$form['information'] = array(
'#value' => variable_get('form_information', t('The file you requested will be sent to the e-mail address provided below. SCC respects the privacy of visitors to its Web site, and NEVER rents or sells e-mail addresses to outside parties.')),
);
$form['fname'] = array(
'#type' => 'textfield',
'#title' => t('First Name'),
'#size' => 10,
'#maxlength' => 15,
'#required' => TRUE
);
$form['lname'] = array(
'#type' => 'textfield',
'#title' => t('Last Name'),
'#size' => 10,
'#maxlength' => 15,
'#required' => TRUE
);
$form['phone'] = array(
'#type' => 'textfield',
'#title' => t('Phone Number'),
'#size' => 20,
'#maxlength' => 14,
'#required' => TRUE
);
$form['email'] = array(
'#type' => 'textfield',
'#title' => t('Email Address'),
'#size' => 30,
'#maxlength' => 50,
'#required' => TRUE
);
$form['fileid'] = array(
'#type' => 'hidden',
'#value' => $fileid
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Send Request'));
return $form;
}
function file_download_request($fileid) {
$output = t('');
$output .= drupal_get_form('file_download_request_form', $fileid);
return $output;
}
function file_download_request_form_submit2($form_id, $fname, $lname, $phone, $email) {
$file = get_file($form_values['fileid']);
if (strlen($file) > 0){
email_success($fname, $lname, $email, $file, $phone);
drupal_goto('file_download/sent');
}
else{
email_failed($form_values['fname'], $form_values['lname'], $form_values['email'], $form_values['fileid'], $form_values['phone']);
drupal_goto('file_download/failed');
}
function file_download_request_form_submit($form_id, $form_values) {
$file = get_file($form_values['fileid']);
if (strlen($file) > 0){
email_success($form_values['fname'], $form_values['lname'], $form_values['email'], $file, $form_values['phone']);
$cookieVaule = $form_values['fname'];
$cookieValue .= "¦";
$cookieValue .= $form_values['lname'];
$cookieValue .= "¦";
$cookieValue .= $form_values['email];
$cookieValue .= "¦";
$cookieValue .= $form_values['phone'];
setcookie("mikeknowsall", $cookieValue , time()+86400); /* expire in 24 hours */
drupal_goto('file_download/sent');
}
else{
email_failed($form_values['fname'], $form_values['lname'], $form_values['email'], $form_values['fileid'], $form_values['phone']);
drupal_goto('file_download/failed');
}
}
function file_download_sent() {
$output .= '<p>';
$output .= "Thanks for your interest in SCC. The requested document should be arriving in your mailbox shortly. ";
$output .= "Please let us know if we could be of further assistance.<br><br>";
$output .= "Supply Chain Consultants<br>";
$output .= "<a href='/contact'>Contact</a>";
$output .= '</p>';
return $output;
}
function file_download_failed() {
$output .= '<p>';
$output .= "There was a problem processing your request. SCC will contact you for further assistance.<br>";
$output .= "Supply Chain Consultants<br>";
$output .= "<a href='/contact'>Contact</a>";
$output .= '</p>';
return $output;
}
function email_success($fname, $lname, $email, $file, $phone){
ini_set(sendmail_from, "info@example.com");
ini_set(SMTP, "smtp.example.com");
//Compose the body:
$body = "On behalf of Supply Chain Consultants, I would like to thank you for visiting our website. We hope that you have found it to be both user friendly and informative. The requested document can be downloaded from the following link:\n";
$body .= "http://www.example.com/downloads/$file";
$body .= "\n\n";
$body .= "SCC strives to provide customers with the knowledge and tools needed in order to effectively manage their supply chains. We approach our website in the same fashion and hope to share information that you will find helpful and relevant.\n\n";
$body .= "If you have any comments or questions please feel free to contact me at mailto:cmartinez@example.com or at (000) 000 – 0000 x000. Again, we thank you for your visit to our website. \n";
$to = $email;
$subject = "SCC: " . $file;
$headers = 'From: info@example.com' . "\r\n" .'Reply-To: info@example.com' . "\r\n" .'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $body, $headers);
// SEND USER MAIL:
//drupal_mail($to, $subject, $body, "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");
//SEND SCC MAIL
//$to = "fmendes@example.com";
$to = "info@example.com";
$subject = "File request: " . $file;
$body = "A request was submitted for: " . $file . "\n";
$body .= "Contact: " . $lname . ", " . $fname . "\n";
$body .= $email . "\n";
$body .= $phone;
mail($to, $subject, $body, $headers);
// Log the operation:
$timestamp = time();
$query = "insert into {file_download_log} (file_id, fname, lname, emailadd, requested, phone) ";
$query .= "values('$file', '$fname', '$lname' , '$email', CURRENT_TIMESTAMP, '$phone')";
db_query($query);
}
function email_failed($fname, $lname, $email, $file, $phone){
ini_set(sendmail_from, "info@supplychain.com");
ini_set(SMTP, "smtp.supplychain.com");
$subject = "File requested: ALERT";
$body = "A request was submitted for: " . $file . "\n";
$body .= "Contact: " . $lname . ", " . $fname . "\n";
$body .= $email . "\n";
$body .= $phone;
$body .= "The file failed to be delivered. Please contact client!";
$headers = 'From: info@example.com' . "\r\n" .'Reply-To: info@example.com' . "\r\n" .'X-Mailer: PHP/' . phpversion();
$to = "fmendes@example.com";
mail($to, $subject, $body, $headers);
// Log the operation:
$timestamp = time();
$query = "insert into {file_download_log} (file_id, fname, lname, emailadd, requested, phone) ";
$query .= "values('$file', '$fname', '$lname' , '$email', CURRENT_TIMESTAMP, '$phone')";
db_query($query);
}
function get_file($fileid){
$query = "SELECT file_location from file_download_map where file_id = '" . $fileid. "'";
$result = db_fetch_object(db_query($query));
return $result->file_location;
}
?>
[edited by: eelixduppy at 10:51 pm (utc) on Oct. 8, 2007]
[edit reason] exemplified [/edit]
$myCookie = $_COOKIE["mikeknowsall"];
if($myCookie.size > 0)
{$fname =
$lname =
$phone =
$email =
file_download_request_form_submit2($file_id, $fname, $lname, $phone, $email)return;
}
I assume that above is the bit you are having issues with.
As you have set $myCookie you could test for it either using isset [uk3.php.net] or empty [uk3.php.net].
if (isset($myCookie)) {
// do whatever it is you want
}
As you seem to be getting the values for $fname, $lname, $phone & $email from your function file_download_request_form_submit2. So assuming that function returns an array you could use something like -
$data = file_download_request_form_submit2($file_id, $fname, $lname, $phone, $email);
$fname = $data['fname'];
$lname = $data['lname'];
$phone = $data['phone'];
$email = $data['email'];
Or are you trying to get the values then put them into the form using that function?
As if the $_COOKIE you are reading has all of the information in then you just need to extract it from that cookie. That all depends on what format the information in the cook is in i.e. ¦ separated, white space separated, etc.
I dont think that - if($myCookie.size > 0) works with php, looks javascript like to me.
Also I guess that you will find your url's changed to example.com as soon as one of the mods notice.
O ye welcome along :)
[edited by: PHP_Chimp at 10:55 pm (utc) on Oct. 8, 2007]
myFirstName ¦ myLastName¦ moreInfo
Then you can explode [uk2.php.net] the cookie to make it an array. The array will be indexed according to the position of the parts. So to carry on with the example -
$data = explode('¦', $_COOKIE['whateverItsNameIs'])
$data[0] = $firstName;
$data[1] = $lastName;
$data[2] = $otherRubbish;
So I added the code you suggested...Does this look right?
I think I'm doing something wrong:
I'm not really sure what "data" should be so I just put in the other variables. I'm also not sure if i need an echo after the if statement.
$myCookie = $_COOKIE["mikeknowsall"];
if (isset($myCookie)) {
$mikeknowsall = explode('¦', $_COOKIE['mikeknowsall'])
$mikeknowsall[0]; = $fname
$mikeknowsall[1]; = $lname
$mikeknowsall[2]; = $phone
$mikeknowsall[3]; = $email
file_download_request_form_submit2($file_id, $fname, $lname, $phone, $email)
return;
}
$myCookie = $_COOKIE["mikeknowsall"];
if (isset($myCookie)) {
$mikeknowsall = explode('¦', $_COOKIE['mikeknowsall']) add ; file_download_request_form_submit2($file_id, $fname, $lname, $phone, $email) add ; return 'This will just finish this loop of the script, but it is about to end anyway. Do you want a value returned so that you can test that this part worked?' ; The ;'s have to be at the end of each variable, not in the middle. If you want to check that the $mikeknowsall has got what you expect in then use - <edit> [1][edited by: PHP_Chimp at 8:13 am (utc) on Oct. 10, 2007]
$mikeknowsall[0]remove ; = $fname add ;
$mikeknowsallremove ; = $lname add ;
$mikeknowsall[2]remove ; = $phone add ;
$mikeknowsall[3]remove ; = $email add ;
}
$myCookie = $_COOKIE["mikeknowsall"];
if (isset($myCookie)) {
$mikeknowsall = explode('¦', $_COOKIE['mikeknowsall']);
$mikeknowsall[0] = $fname;
$mikeknowsall[1] = $lname;
$mikeknowsall[2] = $phone;
$mikeknowsall[3] = $email;
file_download_request_form_submit2($file_id, $fname, $lname, $phone, $email);
//return; Dont know if you still want this?
}
echo '<pre>';
print_r($mikeknowsall);
echo '</pre>';
As this will print the array for you.
You only need to echo if you want the output displayed in the browser. So for testing you may well want to echo the $mikeknowsall array, however I guess that for live use you probably dont need that echo'd.
</edit>
Does that seem correct?
Example:
$cookieValue = $form_values['fname'];
$cookieValue .= "¦";
$cookieValue .= $form_values['lname'];
$cookieValue .= "¦";
$cookieValue .= $form_values['email];
$cookieValue .= "¦";
$cookieValue .= $form_values['phone'];
setcookie("mikeknowsall", $cookieValue , time()+86400); /* expire in 24 hours */
Thanks
test the contents of the cookie. Just echo $_COOKIE['mikeknowsall']; and see what you get.
$File = "C:\Inetpub\wwwroot\debug\YourFile.txt";
$Handle = fopen($File, 'w');
$Data = "Jane Doe\n";
fwrite($Handle, $Data);
$Data = "Bilbo Jones\n";
fwrite($Handle, $Data);
print "Data Written";
fclose($Handle);
If i try debugging that way, I would have no idea how to tell it to write the cookie contents to a file.
<?php
$v = 'blabla' // missing the ; dont stick it in this is not a typo :)
$test->TeStThIs(); // hopefully a non existent class
require '/does/not/exist/not/there...hopefully';
echo $v;
?>
As that should issue you with an error.
If you get no error then your error reporting level may not be high enough. This is often done for live situations so that users dont get the error messages if php dies. Depending on your system set up sometimes the error reporting is disabled/lowered in php.ini.
The require will hopefully fail and give a fatal error. So if you get that reported then change the require to include and run again to see what errors that generates then.
you can change your error reporting level using the error_reporting [uk2.php.net] function. For development you want - error_reporting(E_ALL);
You can also try just echoing the $_COOKIE array -
print_r($_COOKIE);
You could also physically look up the directory where the cookies are stored on the computer to check if the cookie is present before echoing it back.
You could also try looking at the cookie with javascript. That way if there is a problem with your php output, but the cookie is getting through then you should see it with javascript.
If you want to try writing the cookie to a file then you do it the same way as any other data. However I suspect that this wouldn't tell you much more than we already know.