Page 1 of 1

PHP Script to match an URL and reply

Posted: Wed Sep 16, 2009 12:14 am
by Ron_e
I'm a flash guy, I'm no expert in PHP. So please any help is highly appriciated. :D

Question:
When the server side PHP script receives data from a field which will contain the domain name ($thedomain) it should match an email from my email list and send the mail to that perticular email address.
E.g.
The PHP server side script should contain a list of emails
don@meta.com
ryan@webcast.com
maggie@maxka.org

data is sent from http://www.webcast.com/something/something.html to the PHP server script.

End result should be : reply to ryan@webcast.com
I need help in writing the PHP code.

Re: PHP Script to match an URL and reply

Posted: Wed Sep 16, 2009 1:53 am
by Ollie Saunders
So, in your example the user would input "webcast.com" to have an email sent to "ryan@webcast.com"?

Re: PHP Script to match an URL and reply

Posted: Wed Sep 16, 2009 1:57 am
by Ron_e
No. The URL (http://www.webcast.com/something/something.html) will be captured in to a dynamic text box by flash and send to the PHP server side script ($thedomain).

Re: PHP Script to match an URL and reply

Posted: Wed Sep 16, 2009 2:11 am
by Griven
You can use regular expressions to pull the domain out of the URL. A quick Google search pulled this up: http://www.experts-exchange.com/Web_Dev ... 19065.html (scroll to the bottom of the page to actually find the answers)

As far as matching it to the list of email addresses, that might be a bit trickier, especially if you have multiple addresses for the same domain. Unfortunately, I don't know enough about finding strings inside of arrays to be of much assistance there. This may help, though: http://us2.php.net/manual/en/function.array-search.php

Re: PHP Script to match an URL and reply

Posted: Wed Sep 16, 2009 2:22 am
by Ollie Saunders
Ron_e, have you got anything written at all currently? If so, please post it and explain where you're having difficulty. Otherwise I could probably give you an outline of steps that might help you write the solution yourself.

Re: PHP Script to match an URL and reply

Posted: Wed Sep 16, 2009 2:38 am
by Ron_e
I'm not a PHP guy! What I've got so far is the code which captures the text data.

Code: Select all

<?PHP
$to = "mail@mail.com";
 
$subject = "The subject"; 
$headers = "From:" .$email."\r\n"; 
$headers .= "Bcc: $email\r\n"; 
$message = "Name: " . $thename; 
 
$sentOk = mail("$to",$subject,$message,$headers); 
 
echo $_POST["message"]; 
echo "sentOk=" . $sentOk;
I've got no clue in how to achieve it. a sample script would be highly appriciated. :D

Re: PHP Script to match an URL and reply

Posted: Wed Sep 16, 2009 3:23 am
by Ollie Saunders
Okay, so first you're missing a form with the appropriate field in. Google HTML form for information on those.

Re: PHP Script to match an URL and reply

Posted: Wed Sep 16, 2009 4:38 am
by Ron_e
Sorry if I'm confusing you. I'll start all over again.

I've done the form in flash using "LoadVars" and the data is been transfered to the PHP script on the server (PHP code is as in my previous post). It's working fine! The Flash Form even captures the browser URL and sends to the PHP script.

But I don't know how to make the URL match an email address (which is in the PHP script or in an external PHP file) and automatically use it as the senders email address!!!

Re: PHP Script to match an URL and reply

Posted: Wed Sep 16, 2009 5:29 am
by Ollie Saunders
You can use regular expressions to pull the domain out of the URL. A quick Google search pulled this up: http://www.experts-exchange.com/Web_Dev ... 19065.html (scroll to the bottom of the page to actually find the answers)
You can't actually view those things unless you're a member of that site.

Re: PHP Script to match an URL and reply

Posted: Wed Sep 16, 2009 5:38 am
by Ron_e
I guess you are reffering to the expertsexchange.com.

Ollie... can my requirement be done? Please help me in coding it or is there any other method in achieving it?

Re: PHP Script to match an URL and reply

Posted: Wed Sep 16, 2009 5:38 am
by Ollie Saunders
Tested, works:

Code: Select all

<?php
function domainFromUrl($url) { 
   static $protocolSyntax = '://';
   $domainStartPos = strpos($url, $protocolSyntax) + strlen($protocolSyntax);
   $domainEndPos   = strpos($url, '/', $domainStartPos) - $domainStartPos;
   return substr($url, $domainStartPos, $domainEndPos);
}
echo domainFromUrl('http://whacha.com/foo/bar.html'); # outputs: whacha.com
See if you can integrate it yourself.

Useful function: var_dump() shows what data a variable contains.

Re: PHP Script to match an URL and reply

Posted: Wed Sep 16, 2009 5:44 am
by Ron_e
:D Thanks!

I'll try and let you know.

Re: PHP Script to match an URL and reply

Posted: Wed Sep 16, 2009 6:14 am
by Ron_e
I see that it gets the domain name. But that's not necessary. I wanted to match the URL from a list of emails automatically insert it to the TO field.

<?PHP
$to = "$urlmatch";

Please reffer the attached.

Re: PHP Script to match an URL and reply

Posted: Wed Sep 16, 2009 8:20 am
by Ollie Saunders
Oh wow, you did a diagram, lol.

Here's another hint:

Code: Select all

function domainFromEmail($email) { return substr($email, strpos($email, '@') + 1); }
if (domainFromUrl($url) === domainFromEmail($email)) {
   mail($email, ...);
   return;
}