When passing variables in a link to a PHP file, how do I account for spaces in the link?

Okay, I’m further refining my invite system, thanks to all who have helped thus far! Now there is a small issue, what do I do if there is a space in the variable link? Like below:

https://xxxx.net/core/index.php?email2=xxxx@gmail.com&user=Frank Bob

But this only passes “Frank” (and the e-mail). So how do I capture the “Bob” as well?

Thanks!

Perhaps:

urlencode() or rawurlencode() - functions designed to encode URLs for http protocol


$rawEncodeUrl = 'https://somedomain.net/core/index.php?email2=xxxx@gmail.com&user=Frank Bob';
echo 'RAW URL ENCODE: ' . rawurlencode($rawEncodeUrl) . '<br><br>';
echo 'URL ENCODE: ' . urlencode($rawEncodeUrl) . '<br><br>';
RAW URL ENCODE: https%3A%2F%2Fsomedomain.net%2Fcore%2Findex.php%3Femail2%3Dxxxx%40gmail.com%26user%3DFrank%20Bob

URL ENCODE: https%3A%2F%2Fsomedomain.net%2Fcore%2Findex.php%3Femail2%3Dxxxx%40gmail.com%26user%3DFrank+Bob

probably just use it on your parameters… before adding it the URL string.

Or just use quotes around the data…

The real problem is how it is generated, not fixing it afterwards.

I can’t use quotes as it breaks the script. The URL is already inside of a var. I tried single quotes, and it doesn’t work either…

This seems rather complex. lol It’s not a big deal I guess, as the code works, and I can still use it as it. :slight_smile:

I do this:

  $link = $base.$email;
  $who2 = $base.$username;

Then later:

 $message = "https://xxxxx.net/index.php?email2=$link&user=$who2";

If that helps. :slight_smile:

Not complex at all. I use quotes inside links all the time. Always easy and works great.

$rawEncodeUrl = 'https://somedomain.net/core/index.php?email2="xxxx@gmail.com"&user="Frank Bob"';

Note: starting quotes are obvious. Ending, last quote is double-quote then single-quote. Easy.

How do I implement this? Using your example breaks the script. :frowning:

Just use the URL… No encoding needed. You only need to encode it if you are sending special characters.
It should work as-is without any encoding. If for some odd reason you think you need to add encoding, you can use the first version using the rawurlencode() function, but, it is NOT needed.

How are you putting this into your email? Are you sending a plain-text email or a full html-email. These days everyone uses html-email. Therefore, you need to create a link in the email using an “anchor”. This will include the “HREF” which would be your URL. BUT, you need to “ESCAPE” the special characters like quotes… Here is a way around that. Just put the link right into the URL when you create it.
Something like this:

$Url = "<a href='https://somedomain.net/core/index.php?email2=\"xxxx@gmail.com\"&user=\"Frank Bob
"'>Text for reader to see in email...</a>";

Notes: To create a URL LINK in an email, you need to echo it in PHP to the email. But, then, the quotes inside of other quotes need to be ESCAPED. BUT, only if the outer quotes are used again inside the string. This means that if you use DOUBLE-QUOTES at beginning and end of the PHP variable creation, you do not need to escape SINGLE-QUOTES. BUT, you do need to escape DOUBLE-QUOTES inside of other DOUBLE-QUOTES. This is to prevent the outer-and-ending DOUBLE-QUOTES from ending the string before you are finished. Hope that makes sense. Once you create that, you can just echo the $Url variable inside the email wherever you want it.

I realize this can be confusing. Many many people have had issues with quotes inside of other quotes. Something it gets very tricky to sort them out. This one is easy!

1 Like

Okay, it’s starting to make sense. The only issue is that I’m not using “Frank Bob” directly, but the var “$who2”. Does this make a difference?

Are you saying you have a variable with the name inside it called $who2??? If so, create the link this way…

$Url = "<a href='https://somedomain.net/core/index.php?email2=\"xxxx@gmail.com\"&user=\"" . $who2 . "\"'>Text for reader to see in email...</a>";

NOTE: The ESCAPE’d double-quotes are right next to the end quotes where the string is concatenated with the $who2 value.

Yes. I use this in my PHP section:

$message = "https://xxxxx.net/index.php?email2=$link&user=$who2";

And then this is sent as an e-mail to those invited. So the $who2 var needs to be able to handle spaces, because the receiver clicks the link to proceed to the next step (thus passing the vars to the second PHP page).

Try this instead…

But, again, this is just a value for the URL, not the link. Where do you place the $message variable into the email? You might need to change that too…

1 Like

Also, the link needs to be just a text URL, not contained in HTML. It seems to work better for my purposes. However, I can’t get your example to work tho. Can you please show me again without the markup? Also, the email2 var is “$link” which contains the submitted e-mail.

Okay, give me a sec. :slight_smile:

To explain…

The $message you showed is just text. Therefore inside an email it would just be simple text, too.
If an email is sent out from your mail server, it can be just text or HTML formatted. Most all are HTML now.
But, if the $message is being sent out as a text, links work differently. Either way, you need the ending
results to have quotes or double quotes around the values. In the last post I simply added single-quotes
around the values. This should work as long as the $message variable is not sent inside other quotes.
Does that help explain it?

How are you sending out the email? With PHP’s mail() function? If so, this version will work for you.

1 Like

That breaks the script. :frowning: I’m putting the link into a $url var, then inside $message, I do: “Click this link to continue: $url”.

Ah okay. That might be the issue. The $message var is currently this:

$message =  "Proceed to this secure link to continue: $url";

Well, as you see it is inside quotes. So, try this instead:

$message =  "Proceed to this secure link to continue: " . $url;

Note that this ADDs or CONCATENATEs the $url variable to the end of the $message variable.
It does not INSERT it into the text stream. I think that explains it. Just add to it not insert it.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service