PHP如何在url中添加用户ID?

时间:2016-07-07 09:54:49

标签: php mysql

我想知道如何在网址中添加登录用户的ID(例如?id =") 我在Google上搜索了一下,但我找不到任何有用的东西。

这是PHP代码:

<?php 
include("nav.php");
require("app/management/config.php");
session_start();
if(!isset($_SESSION['login_user']))
    {
    header("Location: index");
    }
?>

<!DOCTYPE html>
<html>
<head>
<link href='http://fonts.googleapis.com/css?family=Montserrat:400,700' rel='stylesheet' type='text/css'>
<!--<link rel="stylesheet" href="../css/providers.css">-->
<link rel="stylesheet" href="../css/main.css">
<link rel="stylesheet" href="../css/main2.css">
<meta charset="UTF-8">
<header class="header__wrapper wrapper">
    <a href="/" class="header__habbo__logo">
        <h1 class="header__habbo__name" id="ga-linkid-habbo-large"></h1>
    </a>
</header>
<body>

<?php
include("header.php");
?>

<div class="info-block-homes"> 
<?php
$con = mysqli_connect($host, $username, $password , $database)
or die('Error connecting to MySQL server.'); 
$query = "SELECT * FROM `users` WHERE username = '{$_SESSION['login_user']}'"; 
$data = mysqli_query($con, $query);
$row = mysqli_fetch_array($data);

echo '<h1><font color="Red">Your Public Information:</font></h1>';
echo '<font color="green">' . $motto . '</font><font color="white">' . $row['motto'] . ' </font></br>';
echo '<font color="green">' . $credits . '</font><font color="white">' . $row['credits'] . ' </font></br>';
echo '<font color="green">' . $pixels . '</font><font color="white">' . $row['pixels'] . ' </font></br>';
echo '<font color="green">' . $points . '</font><font color="white">' . $row['points'] . ' </font></br>';
echo '<font color="green">' . $rank . '</font><font color="white">' . $row['rank'] .'</font>';
echo '<div style="margin-top:-130px; margin-left:150px;"><img src="http://www.habbo.com/habbo-imaging/avatarimage?figure='.$row['look'].'\"></div>';
?>
</div>

<div class="badges-block">
<h1><font color="Red">Your Badges:</font></h1>
<?php
$login_session=$_SESSION['login_user'];
$sql = "SELECT
  so.*,
  sr.*
FROM users AS so
INNER JOIN users_badges AS sr ON so.id = sr.user_id WHERE username = '{$_SESSION['login_user']}';";
$result = $dbconfig->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<img src='badges/".$row["badge_code"].".gif'>";
    }
} else {
    echo "0 results";
}
$dbconfig->close();
?>
</div>

<?php
include("footer.php");
?>

<div id="arrow-up-logout-homes" class="mydiv2"><a href="logout"><ul><li><font color="white">Log Out</font></li></ul></a></div>

<?php
$con = mysqli_connect($host, $username, $password , $database)
or die('Error connecting to MySQL server.'); 
$online = "1";
$query = "SELECT count(id) as 'total' FROM `users` WHERE online = '$online'";
$data = mysqli_query($con, $query);
$row = mysqli_fetch_assoc($data);
$online=$row['total'];
echo '<div id="online-me" class="mydiv3"><center><span id="stats">'.$online.' User(s) Online!</span> </center></div>';
?>

<?php
$con = mysqli_connect($host, $username, $password , $database)
or die('Error connecting to MySQL server.'); 
$query = "SELECT * FROM `users` WHERE username = '{$_SESSION['login_user']}'"; 
$data = mysqli_query($con, $query);
$row = mysqli_fetch_array($data);
header('Location: /homes?id='.$row['id']);
exit;
?>

</body>
</head>
</html>

登录页面:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<link href='http://fonts.googleapis.com/css?family=Montserrat:400,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="https://d3hmp0045zy3cs.cloudfront.net/2.2.21/providers.css">
<?php 
require("nav.php");
echo "<title>$title - $desc - $title</title>";
?>
<?php
require("app/management/config.php");
session_start();
if(isset($_SESSION['login_user']))
    {
    header("Location: me");
    }
if($_SERVER["REQUEST_METHOD"] == "POST")
 {
// username and password received from loginform 
$username=mysqli_real_escape_string($dbconfig,$_POST['username']);
$password=mysqli_real_escape_string($dbconfig,$_POST['password']);
$securitykey=mysqli_real_escape_string($dbconfig,$_POST['securitykey']);

$sql_query="SELECT * FROM users WHERE username='$username' and password='$password' and securitykey='$securitykey'";
$result=mysqli_query($dbconfig,$sql_query);
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
$count=mysqli_num_rows($result);


// If result matched $username and $password, table row must be 1 row
if($count==1)
{
$_SESSION['login_user']=$username;

header("location: me");
}
else {
$result = '<div style="position:absolute; margin-left:400px;">Invalid username or password (do not forget the security key if you got one)</div>';
    echo "$result";
}
}
?>
<link rel="stylesheet" href="../css/main.css">
<link rel="stylesheet" href="../css/main2.css">
<head>
<header class="header__wrapper wrapper">
    <a href="/" class="header__habbo__logo">
        <h1 class="header__habbo__name" id="ga-linkid-habbo-large"></h1>
    </a>
</header>
<body>
<div class="login-block">
    <form method="post" action="" name="loginform">
    <input type="text" value="" placeholder="Username" id="username" name="username" />
    <input type="password" value="" placeholder="Password" id="password" name="password" />
    <input type="securitykey" value="" placeholder="Security Key" id="securitykey" name="securitykey" />
    <button type="submit">Submit</button>
    </form>
</div>
<body>
<?php
include("header.php");
?>
<div id="content">
<!--<?php
// Create connection

$sql = "SELECT id, message, username FROM cms_message";
$result = $dbconfig->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo '<marquee behavior="scroll" direction="left">'.$row['username'].' says: '.$row['message'].'</marquee>';
    }
} else {
    echo "0 results";
}
$dbconfig->close();
?>-->
<?php
$con = mysqli_connect($host, $username, $password , $database)
or die('Error connecting to MySQL server.'); 
$online = "1";
$query = "SELECT count(id) as 'total' FROM `users` WHERE online = '$online'";
$data = mysqli_query($con, $query);
$row = mysqli_fetch_assoc($data);
$online=$row['total'];
echo '<div id="online" class="mydiv3"><center><span id="stats">'.$online.' User(s) Online!</span> </center></div>';
?>
<div id="arrow-up" class="mydiv2"><a href="registration"><ul><li><font color="white">Register Now!</font></li></ul></a></div>
</br>
</br>
</br>
</br>
</br>
</br>
<?php

            {
                $con = mysqli_connect($host, $username, $password , $database)
        or die('Error connecting to MySQL server.'); 
$query="SELECT * FROM cms_news WHERE highlighted = '1' AND date IN (SELECT max(date) FROM cms_news)";
$data = mysqli_query($con, $query);
                while($row = mysqli_fetch_array($data))
                {
                    echo '<main class="wrapper wrapper--content" ui-view="">
    <section>
        <h1 translate="NEWS_TITLE">Latest news</h1>
        <div class="main main--fixed">
            <habbo-compile data="NewsController.promos">
                <section>
                    <article class="news-header news-header--column">
                        <a href="'.$row["link"].''.$row["room_id"].'" class="news-header__link news-header__banner">
                            <figure class="news-header__viewport">
                                <img src="'.$row["image"].'" alt="'.$row["image"].'" class="news-header__image news-header__image--featured">
                                <img src="'.$row["thumbnail"].'" alt="'.$row["thumbnail"].'" class="news-header__image news-header__image--thumbnail">
                            </figure>
                        </a>
                        <a href="/hotel?room='.$row["room_id"].'" class="news-header__link news-header__wrapper">
                            <h2 class="news-header__title">'.$row["title"].'</h2>
                        </a>
                        <aside class="news-header__wrapper news-header__info">
                            <time class="news-header__date">'.$row["date"].'</time>
                            <ul class="news-header__categories">
                                <li class="news-header__category">
                                    <a href="/community/category/'.$row["category"].'" class="news-header__category__link" translate="NEWS_CATEGORY_'.$row["category"].'">'.$row["category"].'</a>
                                </li>
                            </ul>
                        </aside>
                        <p class="news-header__wrapper news-header__summary">'.$row["description"].'</p>
                    </article>
    </section>
</main> ';
}
                mysqli_close($con);  
            }
?>
<?php

            {
                $con = mysqli_connect($host, $username, $password , $database)
        or die('Error connecting to MySQL server.'); 
$query="SELECT * FROM cms_news WHERE highlighted = '0'";
$data = mysqli_query($con, $query);
                while($row = mysqli_fetch_array($data))
                {
                    echo '<div style="margin-top:-30px;"><main class="wrapper wrapper--content" ui-view="">
<article class="news-header">
        <a href="/community/article/21340/habboxs-summer-weekend-event" class="news-header__link news-header__banner">
            <figure class="news-header__viewport">
                <img src="'.$row["thumbnail"].'" alt="'.$row["thumbnail"].'" class="news-header__image news-header__image--thumbnail">
            </figure>
        </a>
        <a href="'.$row["link"].''.$row["category"].'" class="news-header__link news-header__wrapper">
            <h2 class="news-header__title">'.$row["title"].'</h2>
        </a>
        <aside class="news-header__wrapper news-header__info">
            <time class="news-header__date">'.$row["date"].'</time>
            <ul class="news-header__categories">
                <li class="news-header__category">
                    <a href="'.$row["link"].''.$row["category"].'" class="news-header__category__link" translate="NEWS_CATEGORY_'.$row["category"].'">'.$row["category"].'</a>
                </li>
            </ul>
        </aside>
        <p class="news-header__wrapper news-header__summary">'.$row["description"].'</p>
    </article>
    </main>
</div>';
}
                mysqli_close($con);  
            }
?>
</div>
</div>

<?php
include("footer.php");
?>
</body>
</head>
</html>

3 个答案:

答案 0 :(得分:1)

尝试:

 header("Location: index?id=".$id);

答案 1 :(得分:0)

正如php.net所说,您需要在exit

之后使用header
<?php
header("Location: http://www.example.com/"); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
exit;
?>

所以请使用以下代码:

header('Location: /index?id='.$row['id']);
exit;

答案 2 :(得分:-1)

使用此代码:

header('Location: index?id='.$row['id']);
exit;