Jump to content

IS THIS RIGHT??


princeofpersia

Recommended Posts

Hi guys, what im trying to do in code below is to avoide public visitors (not registered) to see then content if they are not logged in, but when i visit the site as a public visitor this is the only thing i get Welcome, !

 

in my code below im trying to see if user exist, if not it should show the 

 

echo"You need to login as a user to view this page";

 

can u put me in right direction on how to do this please? thanks in advance

 

 

<?php
session_start();
include ("global.php");

//username session
$_SESSION['username']=='$username';
$username=$_SESSION['username'];

$auth=mysql_query("SELECT username FROM users WHERE username='$username'");
while($row = mysql_fetch_array($auth))
{
$exist_user=$row['username'];
}
if ($username!=$exist_user) 
{
echo"You need to login as a user to view this page";
}
else
{
the rest of the code goes here
}

Link to comment
Share on other sites

I wouldn't say too much is right here...

//username session
$_SESSION['username']=='$username';
$username=$_SESSION['username'];

$auth=mysql_query("SELECT username FROM users WHERE username='$username'");

== is not assigning, but comparing... and there's no "if". I doubt you are trying to compare though...

 

why assign username to username session, then set the username variable from the session, then search in the mysql db after just the username when the username is already known. I think you need to provide more of your code.

 

$query="SELECT username FROM users WHERE username='$username'"
$result=mysql_query($query) or die(mysql_error());
if(mysql_num_rows($result)==1){
  $row=mysql_fetch_assoc($result);
  echo 'Welcome'.$row['username'];
}else{
  echo 'you need to log in';
}

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.