Jump to content

Using "php urls" i.e index.php?option=login


JeBu

Recommended Posts

How it is done?

 

I mean...hm.. 8)

 

for example, i have following HTML code

 

<a href="index.php?option=login">Log in</a>

 

..and a PHP code to switch pages

 

<?php

switch($_GET['option']) {

case "login": {

include("login.php");

break;

default: {

echo "Error 404";

break;

}

}

?>

 

Do I just type a variable in the URL and switch it with php or what?..pff.. :-[

Any kind of help/link/article is welcome

And articles about PHP headers are also welcome

Link to comment
Share on other sites

The idea of url variables is to have a url like:

 

index.php?page=login

 

and then use php to catch the variable and its value with $_GET. Example:

 

if(isset($_GET['page'])){
 echo $_GET['page']; //it will print the page variable, in this case 'login'
} else{
 echo 'You have not set a url variable';
}

 

In this way you can pass variables between different pages or just different parts of your php script.

Link to comment
Share on other sites

The urls are usually generated dynamically from data contained within a database. eg;

 

list_users.php

<?php

  // connect to db
  if ($result = mysql_query("SELECT id,uname FROM users")) {
    if (mysql_num_rows($result)) {
      while($row = mysql_fetch_assoc($result)) {
        echo "<a href='profile.php?id={$row['id']}'>{$row['uname']}</a><br />";
      }
    }
  }

?>

 

profile.php

<?php

  if (isset($_GET['id'])) {
    $id = mysql_real_escape_string($_GET['id']);
    if (mysql_query("SELECT uname,age FROM users WHERE id = '$id' LIMIT 1")) {
      if (mysql_num_rows($result)) {
        $row = mysql_fetch_assoc($result);
        echo "This is the profile of {$row['uname']}, who is {$row['age']} years old";
      }
    }
  }

?>

 

list_users.php list all users names as links, passing each users id through the url. When you click on these links you are taken to profile.php which uses this id to run a query and gather more information about the given user.

 

Hope this helps.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.