Need Help in Url shorting script with adsense revenue sharing

Discussion in 'PHP' started by jonmarko, Oct 20, 2009.

  1. #1
    Hello

    I need URl shorting script with adsense revenue sharing

    can any one help me to got this script :)

    Thanks
     
    jonmarko, Oct 20, 2009 IP
  2. DollaradeConner

    DollaradeConner Active Member Affiliate Manager

    Messages:
    200
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    58
    #2
    This wouldn't be to difficult to do with PHP and MySQL, I'll give a very basic example, but some basic knowledge of both is required.

    First, you'll want to create a table, let's call it 'shorten_table', and add two columns:

    id - INT, primary key, auto-increment
    url - TEXT

    Second, you'll need two PHP scripts. The first one will be used for visitors to enter in their URL they would like to shorten, submit it, then get the shortened link. The second script will be used to redirect visitors using the shortened link to the actual website.

    Below is the first script, assuming your MySQL table is named 'shorten_table' and your domain is 'http://www.your_domain.com':

    submit_link.php
    <?php
    
    /* ////////////////////////////////////////////////////
    Instead of creating two seperate files just to handle
    the user input, it can all be done in one file.  All you
    have to do is check to see if the user and pressed the
    submit button.
    
    The code below check whether the user is visiting this
    page for the first time, or if they have submitted a
    link. 
    /////////////////////////////////////////////////// */
    
    //Include your code to connect to your MySQL database
    //and select your table.  If you don't do this, the
    //code will not work :)
    
    $url = mysql_real_escape_string( $_POST['url'] );
    $submit = $_POST['submit'];
    
    //Success/error handlers
    $success = 0;
    $error = 0;
    
    //Check if user pressed submit
    if( isset( $submit )) {
    	
    	//Make sure $url is not blank
    	if( $url != '' ) {
    		//You would want to perform some type of URL validation here, before query below
    		$sql = mysql_query( "INSERT INTO shorten_table (url) VALUES ('$url')" );
    		
    		if( $sql ) {
    			$shortened_id = mysql_insert_id();
    			$success = 1;
    		} else {
    			$error = 1;
    		}
    	}
    
    }
    
    ?>
    
    <html>
    <head>
    <title>Shorten A Link</title>
    </head>
    
    <body>
    <div style="margin: 0 auto; text-align: center; padding-top: 20px;">
    
    	<?php //If we have successfully shortened a URL, display the shortened URL instead of the form
    
    	if( $success == 1 && isset( $shortened_id )) { ?>
    
    	<p><b>Success! Your shortened link is below:</b></p>
    	<p><a href="http://www.your_domain.com/redirect.php?id=<?php echo $shortened_id; ?>">http://www.your_domain.com/redirect.php?id=<?php echo $shortened_id; ?></a></p>
    
    	<?php } //If there was an error, inform the user
    
    	elseif( $error == 1 ) { ?>
    
    	<p style="color: #ff0000">There has been an error</p>
    
    	<?php } //If it was unsuccessful (or not used yet) and there are no errors, display the form
    
    	else { ?>
    
    	<p><b>Enter Link Below:</b></p>
    
         	<form name="submit_link" action="submit_link.php" method="post">
    		<input type="text" name="url" value="http://" />
    		<input type="submit" name="submit" value="Shorten!" />
    	</form>
    
    	<?php } ?>
    </div>
    
    </body>
    </html>
    PHP:
    Now users need to be redirected when they use the shortened link from your site. It will be used like 'http://www.your_domain.com/redirect.php?id='. Since the id variable is included within the URL, we must use the GET method instead of POST when retrieving it in PHP:

    redirect.php
    <?php
    
    //Get the id of the row for this URL
    $shortened_id = mysql_real_escape_string( $_GET['id'] );
    
    //Get the actual URL from our table (sloppy i know)
    $result_url = mysql_query( "SELECT url FROM shorten_table WHERE id='$shortened_id'" );
    $url_array = mysql_fetch_array( $result_url );
    $url = $url_array['url'];
    
    header( 'Location: ' . $url )
    
    ?>
    PHP:
    This is a quick example, much more can be added to it (and probably should).
     
    DollaradeConner, Oct 20, 2009 IP
  3. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #3
    That was good one DollaradeConner, but I would suggest to keep key or short_url as alphanumeric rather than auto incremented values.
    More over, for precision and avoid duplicates, i would keep it based on current time in microseconds precision :)
     
    mastermunj, Oct 20, 2009 IP
  4. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #4
    You may want to try base_encode with the base 36 to convert numeric into alphanumeric id's.
     
    Gray Fox, Oct 21, 2009 IP