+ Reply to Thread
Results 1 to 5 of 5

Thread: Help creating PHP code

  1. #1
    espfutbol98's Avatar
    espfutbol98 is offline x10 Sophmore espfutbol98 is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Zagreb... želim
    Posts
    200

    Help creating PHP code

    I'm trying to use a script that has usernames, passwords, and ID numbers so when you login, it takes you to your index?[ID number]. I know how to make the id specific page itself (say /123.php) into /index?123 but I don't know how to send just that user there.

  2. #2
    freecrm's Avatar
    freecrm is offline x10 Elder freecrm is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    629

    Re: Help creating PHP code

    I'm not sure you can name individual files simply, but a good shortcut would be the add a variable to the URL.

    For instance, you could specify a redirect to a "home.php" page with a variable.

    "home.php?id=123"

    This means you only have to create one page and can alter that page, depending on the ID number in the URL.

    In the "home.php" file, you could assign the ID number like this.

    <?php

    $id=$_GET['id'];

    ?>

    Then the page can change depending on what $id equals.

    This is how a lot of php sites are created, even though there is actually only one file.

    e.g.

    index.php is the file

    index.php?p=login... refreshes but includes the login page code.

    index.php?p=forum.. refreshes and includes the forum page code.


    As an example..

    <?php
    $id=$_GET['id'];

    if($id=="login"){
    include("includes/login.php");
    }elseif($id=="forum"){
    include("includes/forum.php");
    }

    ?>

    The actual redirection script from your login would be something like

    <?php
    header("Location: whateverfile.php?id=".$memberid);
    ?>

    __________

    Alternatively, if you assign your id to a session, you can call that value at any time, rather than passing it in the URL.

    i.e. $_SESSION['id']

    Hope this helps a bit.
    Last edited by freecrm; 05-04-2009 at 06:02 PM.

  3. #3
    leafypiggy's Avatar
    leafypiggy is offline Community Advocate leafypiggy is on a distinguished road
    Join Date
    Aug 2007
    Location
    Massachusetts
    Posts
    2,228

    Re: Help creating PHP code

    Very secure method:
    PHP Code:
    <?php

    if (isset($_GET[p]) && file_exists($_GET[p].".php"))
    {

        
    $allowedpages = array("idx""band");

        if (
    in_array($_GET[p],$allowedpages))
        {

            include(
    $_GET[p].".php");

        }
        else
        {

            die(
    "Hacking attempt");

        }

    }
    else
    {

        include(
    "idx.php");

    }

    ?>
    Brandon is a glorious beacon of light

    Re: Help
    I am sure some quickly written script like this is more secure than chris z's too

    PHP Code:
    <?php

    if (isset($_GET[p]) && file_exists($_GET[p].".php"))
    {

    $allowedpages = array("idx", "band");

    if (in_array($_GET[p],$allowedpages))
    {

    include($_GET[p].".php");

    }
    else
    {

    die("Hacking attempt");

    }

    }
    else
    {

    include("idx.php");

    }

    ?>
    All you have to do is add the page name to the array, and it'll work, if it's not in the array then it won't.
    Neil Hanlon | x10Hosting Support Representative
    Neil[at]x10hosting.com
    █ I'm always happy to help. Just ask a question in Free Hosting
    Terms of Service IRC

  4. #4
    espfutbol98's Avatar
    espfutbol98 is offline x10 Sophmore espfutbol98 is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Zagreb... želim
    Posts
    200

    Re: Help creating PHP code

    I figured it out. I had to add the user id from the MySQL table to the session cookie so it could be read with $_SESSION['user_id] outside of the database connection ($user_id). Here's the login page:
    PHP Code:
    <?php
    session_start
    ();
    ?>
    <?php
    if (isset($_SESSION['user'])) {
    header("Location: view.php?id=$user_id"); }
    ?>
    <?php 
    include 'dbc.php';

    $user_name mysql_real_escape_string($_POST['name']);

    if (
    $_POST['Submit']=='Login')
    {
    $md5pass md5($_POST['pwd']);
    $sql "SELECT id,user_name FROM users WHERE 
                user_name = '
    $user_name' AND 
                user_pwd = '
    $md5pass' AND user_activated='1'"
                
    $result mysql_query($sql) or die (mysql_error()); 
    $num mysql_num_rows($result);

        if ( 
    $num != ) { 

            
    // A matching row was found - the user is authenticated. 
           
    session_start(); 
           list(
    $user_id,$user_name) = mysql_fetch_row($result);
            
    // this sets variables in the session 
            
    $_SESSION['user']= $user_name AND $_SESSION['user_id']= $id;  
            
                
            if (isset(
    $_GET['ret']) && !empty($_GET['ret']))
            {
            
    header("Location: $_GET[ret]");
            } else
            {
            
    header("Location: view.php?id=$user_id");
            }
            
    //echo "Logged in...";
            
    exit();
        } 

    header("Location: login.php?msg=ERROR: Incorrect username and password."); 
    //echo "Error:";
    exit();        
    }

    ?>

  5. #5
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: Help creating PHP code

    You hade missed some points, and some variables wheren't initialized. Here is an updated script:
    PHP Code:
    <?php
    session_start
    ();

    if (isset(
    $_SESSION['user'])) {
        
    header("Location: view.php?id=" $_SESSION['user_id']);
        exit();
    }

    include 
    'dbc.php';

    $user_name mysql_real_escape_string($_POST['name']);

    if (
    strtolower($_POST['Submit']) == 'login') {
        
    $md5pass md5($_POST['pwd']);
        
    $sql "SELECT id,user_name FROM users WHERE 
            user_name = '
    $user_name' AND 
            user_pwd = '
    $md5pass' AND user_activated='1'"
                
        
    $result mysql_query($sql) or die (mysql_error()); 
        
    $num mysql_num_rows($result);

        if ( 
    $num != ) { 
            list(
    $user_id,$user_name) = mysql_fetch_row($result);
            
    // this sets variables in the session 
            
    $_SESSION['user']= $user_name
            $_SESSION
    ['user_id']= $id;  
                
            if (isset(
    $_GET['ret']) && !empty($_GET['ret'])) {
                
    header("Location: $_GET[ret]");
            } else {
                
    header("Location: view.php?id=$user_id");
            }
            exit();
        } 
        
    header("Location: login.php?msg=ERROR: Incorrect username and password."); 
        exit();        
    }

    ?>
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

+ Reply to Thread

Similar Threads

  1. Hybrid's HTML Lessons
    By Hybrid in forum Tutorials
    Replies: 18
    Last Post: 11-28-2009, 02:12 PM
  2. Replies: 4
    Last Post: 03-15-2009, 01:51 PM
  3. how to add x10 add code on a php page?
    By allinone in forum Scripts & 3rd Party Apps
    Replies: 1
    Last Post: 08-29-2008, 08:20 PM
  4. Replies: 1
    Last Post: 05-08-2008, 04:40 PM
  5. PHP Mail Code Problem
    By Swiblet in forum Free Hosting
    Replies: 2
    Last Post: 11-04-2007, 08:57 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers