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.

Trying to create a global file to call all information

Discussion in 'PHP' started by HellBomb, Sep 11, 2010.

  1. #1
    Ok guys here is my situation, I am new with php, and so far i hate it lol anyways, I am tyring to make a global file that will call several other files to get their information. This way all the rest of the files on the website only have to call the one Global.php file.

    Here is the problem I am working out right now.
    index file is calling global.php
    global.php is calling config.php
    config.php holds the database information
    How Can I make the index file read the database information without linking it directly to the config file.
     
    HellBomb, Sep 11, 2010 IP
  2. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #2
    in the index file it should call

    First 3 lines of index.php
    <?php
    include('global.php');
    ?>

    First 3 lines of global.php
    <?php
    include('config.php');
    ?>

    In the config file you should have the db connection setup and constant otherwise you will have to make the connection on each mysql_query


    Some servers require full paths be present in include, require, require_once instances.
     
    lowridertj, Sep 11, 2010 IP
  3. HellBomb

    HellBomb Active Member

    Messages:
    125
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    80
    #3
    Here is what I have and its not working.

    Config file (location is "public_html/includes/config.php")
    <?php
    
    	// This is the name of the host. On a local server this will be called "localhost" it may change
    	// on other servers but will generally stay the same
    	$host="localhost";
    	
    	// This is the username and password of either the MysSQL database or the name of the account.
    	// either one will work as they both give permission to access teh database. The name and 
    	// password of the account will grant access to any database. If you set up a username and
    	// password on the account via phpmyadmin you can use that username and password for only
    	// a single database. This is a much more secure method.
    	$user="root";
    	$password="";
    	
    	// This is the name of the database. Thiw will somtimes have a prefiv in front of it if you are
    	// using shared hosting such. When you use a local host or a dedicated/private server you do 
    	// not have to worry about it having a prefix.
    	$db_name="FCS";
    ?>
    PHP:
    Global.php (location is "public_html/global.php")
    <?
    include ("includes/config.php");
    ?>
    PHP:
    queries (location is "public_html/admin/queries.php")
    <?php
    
    include ("../global.php");
    
    echo "<html>
          <head><title>SQL Query Sender</title></head>
          <body>";
    if(ini_get("magic_quotes_gpc") == "1")
    {
       $_POST['query'] = stripslashes($_POST['query']);
    }
    /* Section that executes query and displays the results */
    if(!empty($_POST['form']))
    {
      $cxn = mysqli_connect($host,$user,$password,
                            $_POST['database']);
      $result = mysqli_query($cxn,$_POST['query']);
      echo "Database Selected: <b>{$_POST['database']}</b><br>
            Query: <b>{$_POST['query']}</b>
            <h3>Results</h3><hr>";
      if($result == false)
      {
         echo "<h4>Error: ".mysqli_error($cxn)."</h4>";
      }
      elseif(@mysqli_num_rows($result) == 0)
      {
         echo "<h4>Query completed. 
                No results returned.</h4>";
      }
      else
      {
       /* Display results */
         echo "<table border='1'><thead><tr>";
         $finfo = mysqli_fetch_fields($result);
         foreach($finfo as $field)
         {
            echo "<th>".$field->name."</th>";
         }
         echo "</tr></thead>
               <tbody>";
         for ($i=0;$i < mysqli_num_rows($result);$i++)
         {
            echo "<tr>";
            $row = mysqli_fetch_row($result);
            foreach($row as $value)
            {
               echo "<td>".$value."</td>";
            }
            echo "</tr>";
         }
         echo "</tbody></table>";
      } 
     /* Display form with only buttons after results */
      $query = str_replace("'","%&%",$_POST['query']);
      echo "<hr><br>
          <form action='{$_SERVER['PHP_SELF']}' method='POST'>
            <input type='hidden' name='query' value='$query'>
            <input type='hidden' name='database'
                   value={$_POST['database']}>
            <input type='submit' name='queryButton'
                   value='New Query'>
            <input type='submit' name='queryButton'
                   value='Edit Query'>
          </form>";
      exit();
    } 
    
    /* Displays form for query input */
    if (@$_POST['queryButton'] != "Edit Query")
    {
       $query = " ";
    }
    else
    {
       $query = str_replace("%&%","'",$_POST['query']);
    }
    ?>
    <form action="<?php echo $_SERVER['PHP_SELF'] ?>" 
          method="POST">
    <table>
     <tr><td style='text-align: right; font-weight: bold'>
             Type in database name</td> 
         <td><input type="text" name="database"
                value=<?php echo @$_POST['database'] ?> ></td>
     </tr>
     <tr><td style='text-align: right; font-weight: bold' 
             valign="top">Type in SQL query</td>
         <td><textarea name="query" cols="60"
                rows="10"><?php echo $query ?></textarea></td>
     </tr>
     <tr><td colspan="2" style='text-align: center'>
            <input type="submit" value="Submit Query"></td>
     </tr>
    </table>
    <input type="hidden" name="form" value="yes">
    </form>
    </body></html>
    PHP:
     
    HellBomb, Sep 11, 2010 IP
  4. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #4
    in global.php should be

    include ("../includes/config.php");
    or actually best to have it

    include ("/home/user/public_html/path/includes/config.php");

    As right now the global file gets included to a admin file.
    in there it trys to pull /admin/includes/config.php

    just use full path or ../includes/config.php
     
    lowridertj, Sep 11, 2010 IP
  5. HellBomb

    HellBomb Active Member

    Messages:
    125
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    80
    #5
    It's still coming up with nothing, any other ideas as to what I am doing wrong?

    I even tried this path and still won't work
    include ("C:\wamp\www\fcs\includes\config.php");
    PHP:
     
    Last edited: Sep 11, 2010
    HellBomb, Sep 11, 2010 IP
  6. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #6

    should be forward slashes not back slashes.
    <?php include('c:/wamp/www/fcs/includes/config.php'); ?>
    PHP:
    can also try
    <?php include('../includes/config.php'); ?>
    PHP:
     
    lowridertj, Sep 11, 2010 IP
  7. HellBomb

    HellBomb Active Member

    Messages:
    125
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    80
    #7
    ok thank you so much, its cuz I am an ediot, if you look at my global.php you will see i only put <? ?> and I am pretty sure my server doesn't have that short var thing on
     
    HellBomb, Sep 11, 2010 IP
  8. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #8

    everything working well for you now?
     
    lowridertj, Sep 11, 2010 IP
  9. HellBomb

    HellBomb Active Member

    Messages:
    125
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    80
    #9
    Yes thank you so much, only want to ask one more thing. Is their any way that I could set up like a universal variable or something, I know i have seen it before but in the config file I want to set a variable to have the url path such as

    $path="c:/wamp/www/fcs"
    PHP:
    Is something like that possible

    Also any idea why this will not execute?
    $sql = "INSERT INTO login (loginName,loginTime,loginIP)
                       VALUES ('$logname','$today',".$_SERVER['REMOTE_ADDR'].")";
    PHP:
     
    Last edited: Sep 11, 2010
    HellBomb, Sep 11, 2010 IP
  10. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #10

    <?php
    $sql = "INSERT INTO login (loginName,loginTime,loginIP)
                       VALUES ('$logname','$today','".$_SERVER['REMOTE_ADDR']."')"; //add 'and' outside the " and " for the ip insert.  the periods in it is causing it not to execute correctly.
    ?>
    PHP:
    For your global function yes you can make a global include for a folder using php.ini or htaccess
     
    lowridertj, Sep 12, 2010 IP
  11. HellBomb

    HellBomb Active Member

    Messages:
    125
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    80
    #11
    Thank you so much for all your help, I am just starting php and just got stuck with a massive project... anyways you help is greatly apperciated, I will prob be back a billion times trying to get help with stupid php problems. Thank god I am already quite proficient with html and css of this whole process would really be a nightmade.
     
    HellBomb, Sep 12, 2010 IP
  12. HellBomb

    HellBomb Active Member

    Messages:
    125
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    80
    #12
    Ok, now in the same script I am trying to create a remember me box, but it seems that it is not working. Weither or not I have this enabled or not it doesn't log me out when I close the browser. why is that?

    This is the code I can't get to work;
    
    		   $_SESSION['username'] = $_POST['user'];
      		   $_SESSION['password'] = $md5pass;
    		   	if(isset($_POST['remember'])){
          			setcookie("cookname", $_SESSION['fusername'], time()+60*60*24*100, "/");
          			setcookie("cookpass", $_SESSION['fpassword'], time()+60*60*24*100, "/");
      			 }
    
    PHP:
     
    HellBomb, Sep 12, 2010 IP
  13. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #13
    the cookies should be pulling the post of username and password
    stead of fusername and fpassword
    use
    $_POST['user']
    $_POST['password']

    for the cookies.
    Only way you use somethign different is if your username and password text field is different.

    Then simply call
    <?=$cookname?> or <?php echo $cookname;?> in the value for username text bar
    <?=$cookpass?> or <?php echo $cookpass;?> in the value for the password text bar
     
    lowridertj, Sep 12, 2010 IP
  14. HellBomb

    HellBomb Active Member

    Messages:
    125
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    80
    #14
    Think you can give an example code for that little snippet? I am a little confused
     
    HellBomb, Sep 12, 2010 IP
  15. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #15
    put here you login page your using, including form.
    And i can see what I can do.

    TJ
     
    lowridertj, Sep 12, 2010 IP
  16. HellBomb

    HellBomb Active Member

    Messages:
    125
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    80
    #16
    Been using a book to help me get through it and I have been commenting the hell outta my code xD anyways here it is. Also any way I can add a expiration thing? got any links to tutorials or a good method to do it?
     
    Last edited: Sep 12, 2010
    HellBomb, Sep 12, 2010 IP
  17. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #17
    does it login for you?

    also this dont show the login form used?
    the page in which posts the data to this php calling.
     
    lowridertj, Sep 12, 2010 IP
  18. HellBomb

    HellBomb Active Member

    Messages:
    125
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    80
    #18
    opps sorry here it is
     
    Last edited: Sep 12, 2010
    HellBomb, Sep 12, 2010 IP
  19. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #19
    
                            <input name="fusername" class="text-input" type="text" />
                        </p>
                        <div class="clear"></div>
                        <p>
                            <label>Password</label>
                            <input name="fpassword" class="text-input" type="password" />
                        </p>
                        <div class="clear"></div>
                        <p id="remember-password">
                            <input name="remember" type="checkbox" />Remember me
                        </p>
                        <div class="clear"></div>
                        <p>
                            <input type="hidden" name="do" value="login" />
                            <input name="log" class="button" type="submit" value="Sign In" />
    
    HTML:
    update to

    
                            <input name="fusername" class="text-input" type="text"  value="<?php echo $cookuser;?>" />
                        </p>
                        <div class="clear"></div>
                        <p>
                            <label>Password</label>
                            <input name="fpassword" class="text-input" type="password" value="<?php echo $cookpass;?>" />
                        </p>
                        <div class="clear"></div>
                        <p id="remember-password">
                            <input name="remember" type="checkbox" />Remember me
                        </p>
                        <div class="clear"></div>
                        <p>
                            <input type="hidden" name="do" value="login" />
                            <input name="log" class="button" type="submit" value="Sign In" />
    
    HTML:
     
    lowridertj, Sep 12, 2010 IP
  20. HellBomb

    HellBomb Active Member

    Messages:
    125
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    80
    #20
    hmmm now I am getting

    notices undefined variable $cookuser and $cookpass
     
    HellBomb, Sep 13, 2010 IP