/    Sign up×
Community /Pin to ProfileBookmark

check if logged in php issue

Hi

I have managed to get the page checking if logged in already and redirect to the profile page but the url outputs as http://it-doneright.co.uk/affiliate-profile.php?id=$id

I need to get the userid after = instead of $id

Below is my code from the login page, I am unsure of what I am doing wrong or what I am not doing to get the userid in the url, hope someone can help me please

[CODE]
<?php
session_start();
ob_start();

if (!empty($_SESSION[‘username’])) {
header(‘Location:affiliate-profile.php?id=$id’);
exit;
}
?>

<?php
$title = “Affiliate Login – IT Done Right”;

$pgDesc=”IT Done Right are an Laptop repair company based in Pitsea covering Basildon, Laindon and more…”;

$pgKeywords=”laptop repair Pitsea, laptop repair Basildon, laptop repairs Pitsea, laptop repairs Basildon”;

include ( ‘includes/header.php’ );
?>
<!–CONTENT–>

<div id=”column-whole”>
<h2 class=”title”>Affiliate Login</h2>
<hr class=”carved” />

<form action=”” method=”post” class=”signup”>
<h2>Name*:</h2> <input type=”text” name=”username” />
<br>
<h2>Password*:</h2> <input type=”password” name=”password” />
<br>
<input type=”submit” value=”Login” id=”submit” />
<a href=”reset-pass.php”>Forgot Password?</a> | <a href=”new-affiliate-signup.php”>Register</a>
</form>

<?php
include’config-db.php’;
if(!empty($_POST[‘username’]) && !empty($_POST[‘password’]))
{
$username = $_POST[‘username’];
$password = md5($_POST[‘password’]);

//$id = $_GET[‘id’]; Can’t do this, because you don’t pass the ID through GET. You can’t really, as you don’t know the ID when the user logs in.

//check data
//Cant check the ID here either, as you don’t know it yet
$sql = “SELECT * FROM affiliates WHERE username=’$username’ AND password =’$password'”;

$result = $conn->query($sql);
if ($result->num_rows > 0){
while($row = $result->fetch_assoc()) {
$username = $row[“username”];
//Store the name in the session
$_SESSION[‘username’] = $username;
//You should be getting the ID HERE, as this is where you know who the User is.
$id = $row[‘id’];
header(“location:affiliate-profile.php?id=$id”);
exit();
}
}

else {
echo “<h2>Incorrect Username/Password</h2>”;
}
}
?>

</div>

<!–CONTENT–>

<?php include( ‘includes/footer.php’ ); ?>
[/CODE]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogDec 08.2015 — That's coming from the beginning of the script, because you used single quotes instead of double, and variable interpolation does not occur within a single-quoted string:
[code=php]
if (!empty($_SESSION['username'])) {
header('Location:affiliate-profile.php?id=$id');
exit;
}
[/code]

That being said, at that point in the code, how does it know what $id is, anyway?

(On a side note, if you're going to set the user name in $_SESSION, why not just set the user ID there, as well, and not worry about passing it around via URLs?)
Copy linkTweet thisAlerts:
@ianhaneyauthorDec 08.2015 — That's coming from the beginning of the script, because you used single quotes instead of double, and variable interpolation does not occur within a single-quoted string:
[code=php]
if (!empty($_SESSION['username'])) {
header('Location:affiliate-profile.php?id=$id');
exit;
}
[/code]

That being said, at that point in the code, how does it know what $id is, anyway?

(On a side note, if you're going to set the user name in $_SESSION, why not just set the user ID there, as well, and not worry about passing it around via URLs?)[/QUOTE]


Hi

I have changed it a bit since and now have the following

[code=php]
<?php
session_start();
ob_start();

if (!empty($_SESSION['username'])) {
header("Location: affiliate-profile.php?id={$id}");
exit;
}
?>
[/code]


so instead of

[code=php]
if (!empty($_SESSION['username'])) {
[/code]


do you mean have it as the following?

[code=php]
if (!empty($_SESSION['id'])) {
[/code]
Copy linkTweet thisAlerts:
@rootDec 11.2015 — What NogDog is saying is if you are using php sessions, you don't need to tag ID's or anything for that matter in to a URL, you simply set the ID as a session value that the next page can read.

So [code=php] <?php
session_start();
ob_start();

if (!empty($_SESSION['username'])) {
header("Location: affiliate-profile.php?id={$id}");
exit;
}
?> [/code]


becomes [code=php] <?php
session_start();
if ( isset( $_SESSION['username'] ) && !empty( $_SESSION['username'] )) {
$_SESSION['id'] = $id; // set the users ID
header("Location: affiliate-profile.php");
exit;
}
?> [/code]
Copy linkTweet thisAlerts:
@rootDec 11.2015 — Also... Change this "are an Laptop" to "are a Laptop" its minor grammar errors like this that drive OCD grammar Nazi in to action.
×

Success!

Help @ianhaney spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...