PHP Script to match an URL and reply

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ron_e
Forum Newbie
Posts: 13
Joined: Tue Sep 15, 2009 11:59 pm

PHP Script to match an URL and reply

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: PHP Script to match an URL and reply

Post by Ollie Saunders »

So, in your example the user would input "webcast.com" to have an email sent to "ryan@webcast.com"?
Ron_e
Forum Newbie
Posts: 13
Joined: Tue Sep 15, 2009 11:59 pm

Re: PHP Script to match an URL and reply

Post 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).
Griven
Forum Contributor
Posts: 165
Joined: Sat May 09, 2009 8:23 pm

Re: PHP Script to match an URL and reply

Post 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
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: PHP Script to match an URL and reply

Post 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.
Ron_e
Forum Newbie
Posts: 13
Joined: Tue Sep 15, 2009 11:59 pm

Re: PHP Script to match an URL and reply

Post 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
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: PHP Script to match an URL and reply

Post by Ollie Saunders »

Okay, so first you're missing a form with the appropriate field in. Google HTML form for information on those.
Ron_e
Forum Newbie
Posts: 13
Joined: Tue Sep 15, 2009 11:59 pm

Re: PHP Script to match an URL and reply

Post 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!!!
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: PHP Script to match an URL and reply

Post 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.
Ron_e
Forum Newbie
Posts: 13
Joined: Tue Sep 15, 2009 11:59 pm

Re: PHP Script to match an URL and reply

Post 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?
Last edited by Ron_e on Wed Sep 16, 2009 5:42 am, edited 1 time in total.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: PHP Script to match an URL and reply

Post 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.
Ron_e
Forum Newbie
Posts: 13
Joined: Tue Sep 15, 2009 11:59 pm

Re: PHP Script to match an URL and reply

Post by Ron_e »

:D Thanks!

I'll try and let you know.
Ron_e
Forum Newbie
Posts: 13
Joined: Tue Sep 15, 2009 11:59 pm

Re: PHP Script to match an URL and reply

Post 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.
Attachments
question.jpg
question.jpg (36.89 KiB) Viewed 275 times
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: PHP Script to match an URL and reply

Post 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;
}
Post Reply