Skip to content
Advertisement

How to grab url path to file in php

For sending a link to a email (confirming subscription). i need the path to the file subscribe.php. On my webserver, the file subscribe.php is in the webroot. External access will be: http://newsletter.mydomain.com/subscribe.php

This is the link in subscribe.php which i send to the user by email:

$mail->Body = '<a href="'.$subscribe_path.'subscribe.php?name='.urlencode(base64_encode($name)).'&email='.urlencode(base64_encode($email)).'">Confirm your email</a>';  

At this moment i use this:

$subscribe_path = 'http://newsletter/mydomain.com/'

How can i grab the path more generally? i tried:

$subscribe_path = dirname(__FILE__).'/'; 

but that produces something like /home/vhosts/mydomain.com/subdomains/newsletter/httpdocs/

Advertisement

Answer

In file subscribe.php put var below:

  $subscribe_path = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[PHP_SELF]";

When you echo $subscribe_path it will output: http://newsletter.mydomain.com/subscribe.php

Lets say you have subscribe.php in a subfolder, named subfolder, it will output:

http://newsletter.mydomain.com/subfolder/subscribe.php

The link in the email:

$mail->Body = '<a href="'.$subscribe_path.'?name='.urlencode(base64_encode($name)).'&email='.urlencode(base64_encode($email)).'">Confirm your email</a>';   

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement