URI Referrer: How can I redirect to another page based on referring URL?

Posted on 2009-04-20
Last Modified: 2013-12-17
How can I limit visibility and access to a form confirmation page?

I have a form that inserts a record into the database then displays a separate confirmation page saying thanks.  The page does the insert, then uses response.redirect to display the thank you page.  I would like to make it so someone just could not print the thank you page by typing in the url.  How could I make it so that if they type in the url, they go back to the homepage, but, if it is a result of a successful form insert, they see it after the insert is done?

public void PreLoad(object sender, EventArgs e)


        Uri Referrer = Request.UrlReferrer;

        if ((Referrer != null) || (!Referrer.ToString.Equals("default.aspx")))




Open in new window

Question by:devnewbee
    LVL 11

    Accepted Solution

    Instead of using the referrer why not use a session variable to determine if they should have access.  In the code that does the insert add code like below:

    //Code to insert record here
    Session["FormInserted"] = true;

    Then in the page load for the confirmation check

    if (!IsPostBack && Session["FormInserted"] == null)
    LVL 39

    Assisted Solution

    You are quite close. Use:

    string referrerPath = Request.UrlReferrer.GetLeftPart(UriPartial.Path);if(referrerPath.EndsWith("default.aspx")) {    ...}
    instead and you should be fine
    LVL 39

    Expert Comment

    but sessions are a much better idea, as jmwheeler points out :)

    Author Closing Comment

    Thanks you guys.  The assistance here is phenomenal.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now