开发者

Get user name from url and set as string. But its not returning anything

I am trying to return the current username. I get the user name from the URL and access by $_GET['user'] method.

But when i try with the get method it will not retur开发者_StackOverflow社区n the name, instead its returning empty.

echo $_GET['user']; // giving myname

$userName=$_GET["user"];

function getUserName()
{
global $userName;       
    if(isset($userName)){   
        return $userName;
    }else{
        return "defaultuser";
    }
}

I tried this, too:

$userName="".$_GET['user']."";

But when I give simply $userName="myname"; its working. So is there any problem in this line $userName=$_GET["user"];.


Try this

http://www.mysite.com/index.php?user=myname

Method 1 :

if(isset($_GET['user'])) {
    $username = $_GET['user'];
} else {
    $username = 'defaultUser';
}

Method 2 :

function getUsername() {
   $username = (isset($_GET['user'])) ? $_GET['user'] : 'defaultUser';
   return $username;
}

Second Method uses the ternary operator. it does the exact same thing as the first method.

P.S : If you consider using above code for database querying do not forget to use mysql_real_escape_string()


try this:

function getUserName()
{

    if(isset($_GET['user'])){   
        return $_GET['user'];
    }else{
        return "defaultuser";
    }
}  

You need not to have other var, also $_GET is superglobal, so you need not global operator

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜