ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Checking the validity of a Username in a Segment

April 26, 2011 1:39am

Subscribe [2]
  • #1 / Apr 26, 2011 1:39am

    jschutt

    452 posts

    Does anyone know how I could check whether a username that is in segment_2 is actually a legitimate username?  And, if it isn’t, how to redirect to a 404?

    I’ve got the query module checking the DB, but I can’t seem to get the logic correct if it returns nothing.

    {exp:query sql="SELECT exp_members.username AS query_username
                    FROM exp_members
                    WHERE '{segment_2}' = exp_members.username;"}
                    {query_username}
    {/exp:query}

    Thank you!

  • #2 / Apr 26, 2011 9:26am

    Mark Bowen

    12637 posts

    What do you mean by can’t get the logic correct when it returns nothing?

    Have you tried using the {if no_results} tag pair?

    Best wishes,

    Mark

  • #3 / Apr 26, 2011 12:12pm

    jschutt

    452 posts

    Geez… thanks Mark.  I’m not sure how I missed that!

    I did all sorts of stuff trying conditionals and php…

  • #4 / Apr 26, 2011 12:15pm

    jschutt

    452 posts

    Anyhow- here’s the code that is currently working to check if {segment_3} is a valid username.  If not, it redirects to a 404.

    {exp:query sql="
        SELECT  exp_members.username AS query_username
            FROM exp_members
            WHERE '{segment_3}' = exp_members.username
    "}
    
    {if no_results}
        {redirect="404"}
    {/if}
    
    {/exp:query}

    Big thanks to Mark Bowen for pointing out the {if no_results} 😊

  • #5 / Apr 26, 2011 12:21pm

    Mark Bowen

    12637 posts

    Geez… thanks Mark.  I’m not sure how I missed that!

    I did all sorts of stuff trying conditionals and php…

    Excellent news. Glad that got it working for you.

    Best wishes,

    Mark

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases