1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

passing random variable string into url

Discussion in 'PHP & Perl' started by Website, Jun 9, 2010.

  1. Website

    Website Supreme Member

    Joined:
    Feb 8, 2008
    Messages:
    1,291
    Likes Received:
    286
    Location:
    whitehouse
    I have two scripts. One is a tinyurl API and the other gnerates random strings. What I want to do it pass a random string into the tiny URL API script such that the tiny URL generated is always different

    Code:
    <?php
    
    function createRandomPassword() { 
    
        $chars = "abcdefghijkmnopqrstuvwxyz023456789"; 
        srand((double)microtime()*1000000); 
        $i = 0; 
        $pass = '' ; 
    
        while ($i <= 7) { 
            $num = rand() % 33; 
            $tmp = substr($chars, $num, 1); 
            $pass = $pass . $tmp; 
            $i++; 
        } 
    
        return $pass; 
    
    } 
    
    // Usage 
    $password = createRandomPassword(); 
    
    ?>
    
    
    <?php
    
    
    //gets the data from a URL  
    function get_tiny_url($url)  
    {  
    	$ch = curl_init();  
    	$timeout = 5;  
    	curl_setopt($ch,CURLOPT_URL,'http://t1nyurl.com/api-create.php?url='.$url);  
    	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);  
    	curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);  
    	$data = curl_exec($ch);  
    	curl_close($ch);  
    	return $data;  
    }
    
    //test it out!
    $new_url = get_tiny_url('http://domain.com/?password);
    
    
    echo $new_url
    
    ?>
    where it says http://domain.com/?password i want the password value in the first script to be passed before a tiny url is generated

    thanks
     
    Last edited: Jun 9, 2010
  2. voyevoda

    voyevoda Regular Member Premium Member

    Joined:
    Mar 21, 2010
    Messages:
    217
    Likes Received:
    97
    Location:
    Eastern Front
    That's a, uh,... really interesting (and not very good) random string generator! :)

    I think this does what you want:

    PHP:
    <?php

    function createRandomPassword() { 

        
    $chars "abcdefghijkmnopqrstuvwxyz023456789"
        
    srand((double)microtime()*1000000); 
        
    $i 0
        
    $pass '' 

        while (
    $i <= 7) { 
            
    $num rand() % 33
            
    $tmp substr($chars$num1); 
            
    $pass $pass $tmp
            
    $i++; 
        } 

        return 
    $pass



    // Usage 
    $password createRandomPassword(); 

    //gets the data from a URL  
    function get_tiny_url($url)  
    {  
        
    $ch curl_init();  
        
    $timeout 5;  
        
    curl_setopt($ch,CURLOPT_URL,"http://t1nyurl.com/api-create.php?url=$url");  
        
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);  
        
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);  
        
    $data curl_exec($ch);  
        
    curl_close($ch);  
        return 
    $data;  
    }

    //test it out!
    $new_url get_tiny_url("http://domain.com/?$password");


    echo 
    $new_url;

    ?>
    Here's how I would do it:

    PHP:
    <?php

    function get_tiny_url($url)
    {
        
    $ch curl_init();
        
    $timeout 5;
        
    curl_setopt($chCURLOPT_URL"http://t1nyurl.com/api-create.php?url=$url");
        
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
        
    curl_setopt($chCURLOPT_CONNECTTIMEOUT$timeout);
        
    $data curl_exec($ch);
        
    curl_close($ch);
        return 
    $data;
    }

    //test it out!
    $new_url get_tiny_url('http://domain.com/?' uniqid());

    echo 
    $new_url;

    ?>
     
    Last edited: Jun 10, 2010