Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

How do I link in CGI?

by Anonymous Monk
on Jul 11, 2001 at 22:59 UTC ( [id://95811]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi! I am new to Perl and CGI. I am trying to figure out how do I create link, similar to href tag in HTML? Can someone please help me? Thanks P.A.P

Replies are listed 'Best First'.
Re: How do I link in CGI?
by monkeygirl (Pilgrim) on Jul 11, 2001 at 23:36 UTC

    There's two different ways to do it. First, the better way (CGI.pm)

    # This line loads the CGI.pm module. use CGI; # Print any error messages to the browser use CGI::Carp qw(fatalsToBrowser warningsToBrowser); my $im_better = new CGI; # You need to print a content header so you don't end up with Server 5 +00 errors print $im_better->header(); print $im_better->start_html(-title => 'Test Page'); # The coveted link print $im_better->a({href=>"http://www.google.com"}, 'Google'); $im_better->end_html();

    Or you can do it quick and dirty:

    print "Content-type: text/html\n\n"; # The content header ..... # Put stuff you need here print '<a href="http://www.google.com>Google</a>';

    If you're also processing form results, etc., I strongly urge you to use CGI.pm.

    Update: Your reply to E-Bitch leads me to believe you might be talking about redirection. In which case:

    use CGI; my $im_better = new CGI; # Please note that I didn't print a header this time print $im_better->redirect("http://www.google.com");

    And the quick and dirty way:

    # I also didn't print a header this time either print "location:http://www.google.com\n\n";

    Sarah
    If Bill Gates can name a company after his "bedroom" problems, I can have a stupid sig that points it out.
      Sarah wrote:
      "# I also didn't print a header this time either
      print "location:http://www.google.com\n\n";"

      The formal way to do this would be to use "URI:http://...".
      location: is unoffical. CGI.pm uses both.

      Not that you did this wrong but: You may get incorrect results if you use relative URLs, so always use the absolute http://host/path/file URL. Cheers, Steffen
        Going by my ever handy copy of the HTTP/1.1 RFC, "Location: <absolute-uri>" is the correct way to do it; other options, such as "URI: <blah>" are not recommended, and support for these options would be hit or miss...

        --
        RatArsed

Re: How do I link in CGI?
by Abigail (Deacon) on Jul 11, 2001 at 23:18 UTC
    Eh, what exactly is your question? What do you mean by "link"? Do you mean the LINK tag in HTML? You can create that with a print statement, as in:
    print '<LINK HREF = "style.css" REL = "Stylesheet">';
    or you could use the corresponding functions from CGI.pm.

    -- Abigail

Re: How do I link in CGI?
by synapse0 (Pilgrim) on Jul 11, 2001 at 23:22 UTC
    CGI and Perl don't do anything special as far as what gets displayed on the browser. It's all HTML/DHTML/JavaScript/etc... perl processes whatever you need processed and spits out (via print for expample) the HTML that will be displayed. So you could easily have 'print qq(<A href="http://link">link name</a>);'.
    If you're using CGI.pm however, you can user a().. if $q was your cgi object, it would be $q->a({href=>"http://link"},"link name");
    -Syn0
Re: How do I link in CGI?
by E-Bitch (Pilgrim) on Jul 11, 2001 at 23:20 UTC
    Well, I think you need to be slightly more clear in your intended usage of the 'link'. I dont quite follow you on your reasoning for creating a link, and when, programmtically, you would need a "link" a la href tag? If you could be more specific, (explain what you are trying to do), I think an answer can be formulated.


    Thanx
    E-Bitch
      E-Bitch, I have a script that allows users to authenticate, once authenticated/not authenticated users need to be linked to either an error message or an appropriate place. Currently, I am able to get the user to authenticate, however once they do that, they just a message stateing XYZ. Rather that getting that message, I want the user to go somewhere else, lets say, "authenticated.htm". hope this is clearer. Thanks P.A.P
        If you are using basic authentication on the Apache webserver then what you are asking is not possible without recompiling Apache to support custom authentication.

        Apache pops up a little dialogue box when you log in. If your login fails then nothing happens. If your login succeeds then you go where you want to go.

        Those are your choices. If you want a customized error screen you are out of luck unless you recompile.

        Unfortunately, I am rather unfamilar with CGI, but I do know (or think I know) that that would be a redirect (i think). I know with JSPs that you can use response.sendRedirect("somepagehere") (or something similar). I would be surprised if CGI coudnt do the same thing. Anybody help out and confirm this?


        thanks!
        E-Bitch
        adendum: or better yet, you could shoot out a meta redirect tag like this:

        <META HTTP-EQUIV='refresh' CONTENT="0;URL='http://www.whatever.com/sub +mitsuccessful.html'">

        in the head. Its a bit of a hack, but it would work.
Re: How do I link in CGI?
by JP Sama (Hermit) on Jul 12, 2001 at 16:29 UTC
    what about a simple:

    print "Location: http://yourdomain.com/~JohnDoe/index.html\n\n";

    ??? is that what you want?

    well, best regards!
    #!/jpsama/bin/perl -w
    $tks = `mount`;
    $jpsama = $! if $!;
    print $jpsama;
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://95811]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (8)
As of 2024-04-19 08:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found