1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Adding a form variable to a php config file

Discussion in 'PHP' started by SEbasic, Oct 13, 2005.

  1. #1
    OK, So I have a PHP script that I got from another site.

    In that script there is a variable in a config.php file.
    SEMrush
    How easy would it be to replace that variable With information from a form on the site? (The Variable in config.php looks like this)...
    $variable_name = "THIS IS WHAT I WANT TO REPLACE";

    The config file is called every time the script is run, Ideally, I'd like someone to be able to input their variable, then have a cookie put onto their machine with the information they enter into the form, so that they don't have to keep adding the information to the form.

    I think that makes sense...
     
    SEbasic, Oct 13, 2005 IP
    SEMrush
  2. SEbasic

    SEbasic Peon

    Messages:
    6,318
    Likes Received:
    318
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Was I *that* confusing?
     
    SEbasic, Oct 13, 2005 IP
  3. king_cobra

    king_cobra Peon

    Messages:
    373
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #3
    the config file is universal and not user dependent. so how can it be related to cookie? u were indeed confusing. the first part was ok, the second part was ok too, but together they re not at all ok. cud u explain what u exaclty want? i mean.. is it the config file be edited with the form variable value a user inputs (but this will refelet for every other user) or is it that u store a value your user enter in cookie?
     
    king_cobra, Oct 13, 2005 IP
    SEbasic likes this.
  4. nevetS

    nevetS Evolving Dragon

    Messages:
    2,544
    Likes Received:
    211
    Best Answers:
    0
    Trophy Points:
    135
    #4
    Sounds like you have a file that is globally included and you aren't talking about php.ini

    Try out this:
    
    if(!($_POST['post_variable_name'] == NULL)){
    $variable_name = $_POST['post_variable_name'];
    } else {
    $variable_name = 'original_value';
    }
    Code (markup):
    D'oh! I just saw the cookie piece. I don't know the call to get a cookie variable, but just replace $_POST['post_variable_name'] as appropriate.

    I'm thinking something along the lines of:
    
    $cookievar = GetCookie('cookiename')
    if(!($cookievar ==NULL)){
    $variable_name = $cookievar;
    } else {
    $variable_name = 'original_value';
    }
    
    Code (markup):
    Now I'm not sure that GetCookie is actually a function, but IIRC it's something close to that.
     
    nevetS, Oct 13, 2005 IP
  5. johnt

    johnt Peon

    Messages:
    178
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You can get cookie variables from the global $_COOKIES array, so you could use
    if ( isset($_COOKIE['cookiename']) ) {
    $variable_name = $_COOKIE['cookiename'];
    } else {
    $variable_name = 'default';
    }
    
    Code (markup):
    cheers

    johnt
     
    johnt, Oct 13, 2005 IP
    SEbasic likes this.
  6. SEbasic

    SEbasic Peon

    Messages:
    6,318
    Likes Received:
    318
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hehe - this place rules...

    OK, so let me be a little more clear (If I can) ;)

    On index.php, I have a search function.
    This search function uses the google API, which is currently hard coded into the config.php file(where I mentioned $variable_name = "THIS IS WHAT I WANT TO REPLACE";).

    I want users to be able to enter their own API key when they visit index.php, but only have to enter it once in order to be able use the search function multiple times... This is why I guessed a cookie would be the best option...

    Does that make sense now? (I hope so) ;)

    I'm looking at the code aove now - will this do the job?

    (You must spread some Reputation around before giving it to nevetS again. Sorry)
     
    SEbasic, Oct 13, 2005 IP
  7. johnt

    johnt Peon

    Messages:
    178
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #7
    The above code will kind of do the job, but you'll want to replace $variable_name='default'; with some code to prompt the user for their API key and then store it in a cookie.
     
    johnt, Oct 13, 2005 IP
  8. SEbasic

    SEbasic Peon

    Messages:
    6,318
    Likes Received:
    318
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Sorry, I'm lost then...

    In the index.php file, what do I use to catch the information input in the form...

    Something like...
    <form method="POST" action="config.php">
    <input name="api" type="text" size="20" class="whatever">
    <input name="Submit" type="submit" class="whatever">
    </p>

    And in the config.php, I add

    if(!($_POST['post_variable_name'] == NULL)){
    $variable_name = $_POST['api'];
    } else {
    $variable_name = 'original_value';
    }
    if ( isset($_COOKIE['apicookie']) ) {
    $variable_name = $_COOKIE['apicookie'];
    } else {
    $variable_name = 'default';
    }

    It that anywhere near right?

    Sorry, I'm really shit at this...
     
    SEbasic, Oct 13, 2005 IP
  9. johnt

    johnt Peon

    Messages:
    178
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #9
    <?php
    	ob_start();
    	include "config.php";
    	if ( isset($_REQUEST['submit']) ) {
    		if ( !isset($_COOKIE['variable_name']) ) {
    			$variable_name = $_REQUEST['variable_name'];
    			setcookie('variable_name', $variable_name);
    		} else {
    			$variable_name = $_COOKIE['variable_name'];
    		}
    		ob_end_flush();
    		// Process the rest of your script
    
    	} else { 
    ?>
    		<form name="form1" method="POST" action="index.php">
    		<!-- Any form elements you need -->
    <?
    		if ( !isset($_COOKIE['variable_name']) ) {
    			echo "<input type='text' name='variable_name' size='20'>\n";
    		}
    ?>
    		<input name="submit" value="Submit" type="submit">
    <?
    	}
    ?>
    PHP:
    I think that should do it when you add the rest of your script bits in

    cheers

    John
     
    johnt, Oct 13, 2005 IP
  10. SEbasic

    SEbasic Peon

    Messages:
    6,318
    Likes Received:
    318
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Weird - this didn't show up in my CP - Thanks, I'll try it in a bit... :)

    Really appreciate it...
     
    SEbasic, Oct 14, 2005 IP