473,383 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

How to collect OS type from user agent?

As a total newbie I find myself in a pickle.
On my website I had a php script that emailed me the details from a form and also included the USER AGENT so I could tell whether they were on Mac or PC. Thats what I need to know in addition to the regular form fields.

I cannot work out how to collect that information and send it as part of the email I receive from the form.

I am sure it is simple but so am I !

Can anyone put me out of my misery.

PS even if I search the internet and find $_SERVER["USER_AGENT"]I do not know how to put it in the PHP script nor how to send the results to my email.

Thanks in advance for any help.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     /* 
  3.     =============================================
  4.     Sendmail.php - send an email from a web form. Make sure this file is called sendmail.php
  5.     when you upload it, otherwise the example form won't find the script and will error.
  6.  
  7.     NOTE: This script is heavily commented. Text after double slashes // is ignored by PHP
  8.     =============================================
  9.     */
  10.  
  11.     // You only need to modify the following three lines of code to customise your form to mail script.
  12.     $email_to = "me@my-site.co.uk";             // Specify the email address you want to send the mail to.
  13.     $email_subject = "1-Enquiry from web form";     // Set the subject of your email.
  14.     // Specify a page on your website to display a thankyou message when the mail is sent
  15.     $thankyou_url = "http://www.my-site.co.uk/confirm.htm";
  16.  
  17.     // Get the details the user entered into the form
  18.     $name = $_POST["name"];
  19.     $email_from = $_POST["email"];
  20.     $message = $_POST["message"];
  21.  
  22.  
  23.  
  24.     // Validate the email address entered by the user
  25.     if(!filter_var($email_from, FILTER_VALIDATE_EMAIL)) {
  26.         // Invalid email address
  27.         die("The email address entered is invalid.");
  28.     }
  29.  
  30.     // The code below creates the email headers, so the email appears to be from the email address filled out in the previous form.
  31.     // NOTE: The \r\n is the code to use a new line.
  32.     $headers  = "From: $email_from . \r\n";
  33.     $headers .= "Reply-To: $email_from . \r\n";    // (You can change the reply email address here if you want to.)
  34.  
  35. // Now we can construct the email body which will contain the name and message entered by the user
  36.  
  37. foreach($_POST as $key => $value) { 
  38.  if ($value) { 
  39.  $message .= $key . " = " . $value . "\r"; 
  40.  } 
  41.  } 
  42.  
  43.  
  44.     // This is the important ini_set command which sets the sendmail_from address, without this the email won't send.
  45.     ini_set("sendmail_from", $email_from);
  46.  
  47.     // Now we can send the mail we've constructed using the mail() function.
  48.     // NOTE: You must use the "-f" parameter on Fasthosts' system, without this the email won't send.
  49.     $sent = mail($email_to, $email_subject, $message, $headers, "-f" . $email_from);
  50.  
  51.     // If the mail() function above successfully sent the mail, $sent will be true.
  52.     if($sent) {
  53.         header("Location: " . $thankyou_url);     // Redirect customer to thankyou page
  54.     } else {
  55.         // The mail didn't send, display an error.
  56.         echo "There has been an error sending your message. Please try later.";
  57.     }
  58.  
  59. ?>
  60.  
Mar 10 '11 #1

✓ answered by JKing

No, sorry still having my coffee not quite awake yet.

Should have been:
Expand|Select|Wrap|Line Numbers
  1. $message .= "User Agent: " . $_SERVER['HTTP_USER_AGENT'];
  2.  

5 2349
JKing
1,206 Expert 1GB
$_SERVER["USER_AGENT"] is a variable belonging to the server collection.

To add it to the email you will need to add it to the $message string.

Expand|Select|Wrap|Line Numbers
  1. $message .= "User Agent: " . $_SERVER['USER_AGENT'];
  2.  
This should be placed at line 42.
Mar 10 '11 #2
Thanks JKing.

I have tried it and the email I get says User Agent:

But noting else.

Have I missed something here?
Mar 10 '11 #3
JKing
1,206 Expert 1GB
No, sorry still having my coffee not quite awake yet.

Should have been:
Expand|Select|Wrap|Line Numbers
  1. $message .= "User Agent: " . $_SERVER['HTTP_USER_AGENT'];
  2.  
Mar 10 '11 #4
Tried it and it works beautifully !!

Many thanks and enjoy your coffee!!!
Mar 10 '11 #5
JKing
1,206 Expert 1GB
Thanks haha! Glad I could help, have a great day.
Mar 10 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: PhilB | last post by:
Hello experts, I am a complete beginner in C++ (although I know C). I am trying to compile the code below, and I get the following error. Can anyone explain to me my mistake? Thanks! PhilB ...
11
by: Phil Newman | last post by:
I'm trying to implement a basic sinewave signal using arrays, but I'm having difficulty (I'm a complete beginner!) This is what I have so far: using namespace std; double signal(double...
1
by: Jim H | last post by:
I coded my first asp form and attempted to display a text message from a component. The static text "Our component says:" shows on the aspx page, but the text line from the component does not show....
12
by: Joshua Rulz | last post by:
Hi, i want to learn to program im quite skilled with computers and want to learn c++. is there anyone who can teach me or tell me a good website to learn it? all replies will be appreciated.
2
by: one man army | last post by:
Hi All- (complete beginner in PHP, using example code written by Rasmus from Cool Toys blog) I have copied a script into a working directory (unix), it includes another script (verbatum from...
5
by: steve young | last post by:
Hi, a complete idiot would like some help with the following: I have 17 command buttons, which I would like to assign 17 text boxes to. The boxes are to be hidden, until the appropriate button is...
4
by: chris19845 | last post by:
Hi there, I am a complete beginner so apologies if I make not a lot of sense. What I am trying to do is write a simple class, it has a class array variable named test of type String, ie private...
2
by: andrewtayloruk | last post by:
I've been given the following code that should make a skyscraper advert appear on my website. The problem i'm having is that it's Javascript and i have never touched it before. I'm guessing i need to...
1
by: hekmat | last post by:
Hey all, I understand this forum isn't really meant as a training ground for new programmers, but I can't find the answer to my question online or any fo my textbooks so I figured a forum post was...
1
by: Suzy | last post by:
Hi I'm currently an enthusiast for vb6 but quite prepared to learn dotnet. Is the transition a hard one? Can anyone recommend a complete beginner's book, preferable available in Australia?...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.