Aayaan Aayaan - 25 days ago 6
MySQL Question

Browser URL issue because special character like "&"

I have a website using PHP and connected with a MySQL database. I also have an admindashboard for control the users with some features in PHP script I have coded.

<a href="../loginUser2.php?userName=<?php echo $SQLRow["userid"];?>" target="_blank" class="a">Records</a>


This code make a login URL link like this when complete with username

mydomain.com/loginUser2.php?userName=abc&xyz


In mysql from table take username abc&xyz or any user. everything is fine just in web browser when I click link for any user then user is automatically login with help of PHP. But when in any user name use symbol or special character LIKE currently i have issue in "&" symbol. when I edit the URL with my self and in web browser when i type manually link with replace of & with %26 link work and user login successfully. i just want if any how user is registered in MySQL database with contain any special character symbol like & or etc so how can I fix web browser work automatically handle this type of symbols and characters.

I tried one stupid thing in phpMyAdmin edit username column and update username like this abc%26xyz I thought when I will update then its works like &=%26 but I am fail.

Answer Source

you can use urlencode() to send special characters and urldecode() to get encoded value.

<a href="../loginUser2.php?userName=<?php echo urlencode($SQLRow["userid"]);?>" target="_blank" class="a">Records</a>

urldecode($_GET['userName']);