PHP Profiles using template of /profile/name

2019-08-12 00:41发布

I am making a website that needs to have a profile system.

Currently i have it setup so you go to /profile/namehere and the profile will show with the information dynamically, BUT ofcourse you have to manually copy paste these profiles and change the name for them to work.

I want it so that when a form is submitted in this case you enter name and press submit they are added to the database which I already have done myself and a profile is automatically made.

I have researched this a little and come up with the fact that I need to make a controller or profile template that will call the information into, but i have already sort of done this, it is just i am unsure and cannot find any documentation online of how to do it specifically with /profile/namehere/

Any help would be extremely appreciated or code examples, thanks so much!

The database i am using is MYSQL although i will be migrating to PDO when i get round to it, but the profiles for now can be in MYSQL. my current page code looks something like this:

Title and meta tags are shown via config file / api. Page content is echoed for the things being selected from database i am using $row['var']; with the select statement to access database.

At this current state a profile is copied and pasted with the name changed to the users name and that is the profile done, i want it so when you submit form to make users name it will automatically create the profile which can be accessed at /profile/namehere/ rather than manual creation.

Conclusion

Essentially, - Form submitted - User visits /profile/name - Template is grabbed and information is called from database dynamically. *

1条回答
一夜七次
2楼-- · 2019-08-12 01:41

You question is a bit broad. So I'll answer as mush as I can understand.

  1. To access to /profile/user, you have to use URL-rewriting. You have to enable the mod_rewrite of Apache (if it's your webserver). Then you have to define rules in a .htaccess file.

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^profile/(\w+)$ profile.php?user=$1
    </IfModule>
    

    So, browsing to http://host/profile/namehere will use profile.php and $_GET['user'] will be defined with "namehere".

  2. Display informations in profile/namehere page (profile.php untested):

    <?php
    if (!isset($_GET['user']))
    {
        // Error: Fall in error 404, or display error, or ...
        header("HTTP/1.1 404 Not Found");
        exit(0);
    }
    
    $username = $_GET['user'];
    
    // connect database using PDO
    $pdo = new PDO('mysql:dbname=DBNAME;host=localhost' , 'DBUSER', 'DBPASS') ;
    
    $sth = $pdo->prepare("select * from profile_table where username=:username") ;
    if (!$sth) { /* Error */ die ; }
    $sth->execute([':username' => $username]) ;
    $result = $sth->fetch(PDO::FETCH_OBJ) ;
    if (!$result) { /* Error */ die ; }
    
    // display informations using template
    header("Content-Type: text/html; charset=utf-8") ;
    include("template.php") ;
    exit();
    
  3. Template example :

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title><?php echo $result->name ?></title>
    </head>
    <body>
        <p><?php echo $result->info1 ?></p>
        <p><?php echo $result->info2 ?></p>
        <p><?php echo $result->info3 ?></p>
    </body>
    </html>
    
查看更多
登录 后发表回答