Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Multiple Questions

authentication

  • Please log in to reply
7 replies to this topic

#1 HarrieWheatley

HarrieWheatley

    CC Newcomer

  • Member
  • PipPip
  • 16 posts
  • Programming Language:PHP

Posted 24 May 2012 - 02:00 PM

Hey

I have multiple questions, so I decided to put them in one post.

1) Can I find any social network scripts anywhere? OR if anybody has used elgg, can I just nitpick scripts from that?
2) Can images, videos and audio be stored in a mysql table for things such as status updates?
3) Does this code connect to the database? If not, what do I need to add?
<?php
/**
* Elgg login action
*
* @package Elgg.Core
* @subpackage User.Authentication
*/
// set forward url
if (isset($_SESSION['last_forward_from']) && $_SESSION['last_forward_from']) {
$forward_url = $_SESSION['last_forward_from'];
unset($_SESSION['last_forward_from']);
} elseif (get_input('returntoreferer')) {
$forward_url = REFERER;
} else {
// forward to main index page
$forward_url = '';
}
$username = get_input('username');
$password = get_input('password', null, false);
$persistent = get_input("persistent", false);
$result = false;
if (empty($username) || empty($password)) {
register_error(elgg_echo('login:empty'));
forward();
}
// check if logging in with email address
if (strpos($username, '@') !== false && ($users = get_user_by_email($username))) {
$username = $users[0]->username;
}
$result = elgg_authenticate($username, $password);
if ($result !== true) {
register_error($result);
forward(REFERER);
}
$user = get_user_by_username($username);
if (!$user) {
register_error(elgg_echo('login:baduser'));
forward(REFERER);
}
try {
login($user, $persistent);
// re-register at least the core language file for users with language other than site default
register_translations(dirname(dirname(__FILE__)) . "/languages/");
} catch (LoginException $e) {
register_error($e->getMessage());
forward(REFERER);
}
// elgg_echo() caches the language and does not provide a way to change the language.
// @todo we need to use the config object to store this so that the current language
// can be changed. Refs #4171
if ($user->language) {
$message = elgg_echo('loginok', array(), $user->language);
} else {
$message = elgg_echo('loginok');
}
system_message($message);
forward($forward_url);
4) I will update this post when I remember the other questions :)

Thanks,
Harrie Wheatley
  • 0

#2 papabear

papabear

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 472 posts
  • Location:DarkSide

Posted 24 May 2012 - 07:38 PM

1. If you are planning to create your own unique social networking site, I don't recommend you use open source script because they aren't unique and a lot of user was using it and it's open source so people who wants to exploit the code will have to study and search for an area where they can exploit it. There is also Dolphin social networking script for free and if you have some budget.. buy a license for socialEngine.

2. Yes, images and videos can be stored into a database you just have to chunk it. If you don't know how to chunk you can just save the videos and images into a folder and just store the file path of the video/images.

3. That snippet of code is not complete it was just the action, elgg is programmed object oriented so be aware that all files and scripts are link together to work as one. you can't make it work if you will just get that. If you can see there are some user defined functions that the programmer use. Editing someone's code is not good, you'll have to study the style on how he/she code it before you can understand the whole flow. If you have the idea and the design. I suggest you create your own. Just learn to design first before you code.. The design I mean was not the application design or the graphical user interface design. but the Flow of the system like the Database Structure Design and Data Flow Diagram or a Flow Chart.

Happy Coding.
  • 0
Life has no CTRL+Z
Never Forget To HIT "LIKE" If I Helped

#3 HarrieWheatley

HarrieWheatley

    CC Newcomer

  • Member
  • PipPip
  • 16 posts
  • Programming Language:PHP

Posted 25 May 2012 - 12:04 PM

Thanks papa bear!

I have another question: when my users log in, I try to get their username displayed, but when the site is logged in to, it says "You have logged in successfully, your username is." What I'm getting at is that it doesn't show the username.. here's the code:

<?
session_start();
if(!session_is_registered(myemail)){
header("location:main_login.php");
}
?>
<html>
<body>
Login Successful you're username is <? $myemail ?>
</body>
</html>

  • 0

#4 papabear

papabear

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 472 posts
  • Location:DarkSide

Posted 26 May 2012 - 02:35 AM

hello harrie you can't do what you want by just storing into a simple variable.. except if that variable is global..
a good way of storing a username and using it is to use of SESSIONS. Here's an example.

This is the code for the login
<?php
	session_start();
	if the username and password is correct then
	$_SESSION['myemail'] = $username;
?>


and here's the code when the user is logged in successfully
<?php
   session_start();
   welcome <?php $_SESSION['myemail']; ?>
?>

Note: the code is not in correct syntax.. It's more like a pseudocode just to help you out.

All in all, SESSIONS are global variable that you can use :)
you can store some data that you might use later on the code, Online Shopping Carts makes use of this to store the product in carts. You can always erase all the data in SESSIONS by just closing the browser or just using the session_destroy() or the unset(); function.

Happy Coding :)
  • 0
Life has no CTRL+Z
Never Forget To HIT "LIKE" If I Helped

#5 HarrieWheatley

HarrieWheatley

    CC Newcomer

  • Member
  • PipPip
  • 16 posts
  • Programming Language:PHP

Posted 26 May 2012 - 01:01 PM

Thanks, that helped a lot. I actually went a step further and created a sessioninfo.php which I used to define the variables and use as an Include code on every page that needed it. Okay, now I have 2 more questions, you're SUCH A HELP by the way :)

1) What code can I include so that If a person tries to view a user-only page, it redirects them to a you must log in page. What is the code for this?
2) If the user tries to visit homepage whilst they are logged in/ session is running it redirects them to a chosen page?

Thanks so much!
  • 0

#6 papabear

papabear

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 472 posts
  • Location:DarkSide

Posted 26 May 2012 - 01:48 PM

after you assigned a value into the $_SESSION variable when the user entered a correct username and password.
You can then check if they're login.

//check if user is logged in
if ( isset($_SESSION['myemail']) ) {
	   //if he/she's login redirect him into the page that you want.
	   header( "Location: site.com/page.php" );
} else {
	  // if he's not logged in. show him this message
	   echo "You are not authorize to view this page. Please login";
	   exit;
}

exit;

is a good function that you can use to exit the program from execution. When PHP notices the exit.. It will not execute all the code below of that. I often use this exit in my codes here's how I check if the user is logged in.

At the very top of my codes I have a condition

if ( !( isset( $_SESSION['username'] ) ) ) {
       echo "Login first before you see this page";
       exit;
}

//all other codes go here

what I did is I first check if the user is logged in.. if he's not logged in, I will display a message and terminate the code from executing so he will just see the error message. If he's logged in. I will let the code continue to execute :)
Happy Coding.
  • 0
Life has no CTRL+Z
Never Forget To HIT "LIKE" If I Helped

#7 HarrieWheatley

HarrieWheatley

    CC Newcomer

  • Member
  • PipPip
  • 16 posts
  • Programming Language:PHP

Posted 27 May 2012 - 11:34 AM

Hey! I've tried this multiple times, but to no avail. Using the top code, which I inserted into an auth.php and used the include command. I'll post the codes. The problem is that even when I'm logged in, I still get the no access message.

Here's the AUTH.php
<?php

session_start();
include("connect.php");
$tbl_name="users"; // Table name
if ( isset($_SESSION['myemail']) ) {
		   //if he/she's login redirect him into the page that you want.
		   header( "Location:feed.php" );
} else {
		  // if he's not logged in. show him this message
		   echo "You are not authorized to view this page. Please login";
		   exit;
}
?>

And here's the feed.php when I try to use the code within the script it's self.
<?php

session_start();
include("connect.php");
$tbl_name="users"; // Table name
session_start();
include("connect.php");
$tbl_name="users"; // Table name
if ( isset($_SESSION['myemail']) ) {
		   //if he/she's login redirect him into the page that you want.
		   header( "Location:feed.php" );
} else {
		  // if he's not logged in. show him this message
		   echo "You are not authorized to view this page. Please login";
		   exit;
}

Print "Good morning $myname, welcome to your $mytype profile. You are $mygender and your email is $myemail <a href='logout.php'>LOGOUT</a>";
?>

Any ideas? Thanks!
  • 0

#8 papabear

papabear

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 472 posts
  • Location:DarkSide

Posted 27 May 2012 - 04:18 PM

you used two checking if he's loggin? and please make sure that you have a variable $_SESSION['myemail'] because that's only my example :)
you already check at the top so you don't have to check it again. Is this the ELGG that you are still using? I suggest you code your own :) because editing
of codes would be hard to do and also I suggest you try to read the use of SESSIONS in php first and how to use it correctly. Have fun :)
  • 0
Life has no CTRL+Z
Never Forget To HIT "LIKE" If I Helped





Also tagged with one or more of these keywords: authentication

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download