is this possible with php? I have a login script and i need a way to check if the session exists so users can view pictures in the directory

IN your Login script how you are checking?

Using database right?

If the user has authenticated then create a session id. Wherever you want to give access to the logged in users then check for session id.

After logging out the user, destroy his session

IN your Login script how you are checking?

Using database right?

If the user has authenticated then create a session id. Wherever you want to give access to the logged in users then check for session id.

After logging out the user, destroy his session

how can you create a session for that person logged in to be able to view images? I mean any guest can browse and guess picture urls and see them without being logged in right now

Member Avatar for diafol
if(isset($_SESSION['mysite']['user'])){
  $imgRights = 1;
}

then where the images are output to the screen:

<?php if($imgRights){?>
<img src="myimage.png" />
<?php } ?>

There are loads of ways of doing this.

if(isset($_SESSION['mysite']['user'])){
  $imgRights = 1;
}

then where the images are output to the screen:

<?php if($imgRights){?>
<img src="myimage.png" />
<?php } ?>

There are loads of ways of doing this.

Would that stop direct url access of images in the directory?

I've tried loads of ways to stop direct url access to images none have worked so far. I only want the images to be viewed if you're logged in.

Member Avatar for diafol

No it wouldn't if they KNEW the url, but if they are only displayed to logged in users, they may be difficult to figure out.

To do what you want, I use a php file to display an image. The php file looks for a session variable. If it is set - show the requested image. Otherwise, show nothing or a default/alternative image.

In order to do this, I keep a list of images in a DB with an id. The id is passed to the php page thus:

<img src="image.php?id=23" />

The image.php file:

1) check for session login variable
2) get id from url
3) get filename from DB
4) create image header

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.